the thing is that im making a combat system, and there is the situation with line 56 that says: enemyHumanoid:TakeDamage(Damage) and the error says: ServerScriptService.CombatSystem:56: attempt to index nil with 'TakeDamage' and i dont know what to do (also this is the entire code)
local rp = game:GetService("ReplicatedStorage")
local Combat = rp:WaitForChild("Combat")
local Debris = game:GetService("Debris")
local Animations = script:WaitForChild("Animations")
local Meshes = script:WaitForChild("Meshes")
local anims =
{
Animations:WaitForChild("Right"),
Animations:WaitForChild("Left"),
Animations:WaitForChild("Gut"),
Animations:WaitForChild("Kick"),
}
local limbs =
{
"RightHand",
"LeftHand",
"RightHand",
"RightFoot"
}
local Damage = 10
Combat.OnServerEvent:Connect(function(player,count)
local Character = player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local attack = Humanoid:LoadAnimation(anims[count])
attack:Play()
local Limb = Character:WaitForChild(limbs[count])
local folder = Instance.new("Folder",Character)
folder.Name = player.Name.."Melee"
local Hitbox = Meshes:WaitForChild("Hitbox"):Clone()
Hitbox.CFrame = Limb.CFrame
Hitbox.Parent = folder
Debris:AddItem(Hitbox,.5)
local weld = Instance.new("ManualWeld")
weld.Part0 = Hitbox
weld.Part1 = Limb
weld.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
weld.Parent = weld.Part0
Hitbox.Touched:Connect(function(Hit)
if Hit:IsA("BasePart") then
if not Hit:IsDescendantOf(Character) then
local enemyHumanoid = Hitbox.Parent:FindFirstChild("Humanoid")
if Humanoid then
Hitbox:Destroy()
enemyHumanoid:TakeDamage(Damage)
end
end
end
end)
Combat:FireClient(player)
end)
i haven't try a lot but it keeps getting errors
Welcome to StackOverflow! :FindFirstChild("Humanoid") returns nil if the Humanoid doesn't exist, which is why enemyHumanoid:TakeDamage() isn't working. Try using this snippet in place of the current Hitbox.Touched function:
Hitbox.Touched:Connect(function(Hit)
if Hit:IsA("BasePart") and not Hit:IsDescendantOf(Character) then
local enemyHumanoid = Hitbox.Parent:FindFirstChild("Humanoid")
if Humanoid then
Hitbox:Destroy()
if enemyHumanoid then -- Make sure enemyHumanoid exists and isn't nil
enemyHumanoid:TakeDamage(Damage)
end
end
end
end)
Related
I'm trying to get the Range Rate data between a satellite and ground site and everything works up until the last line. I've followed the online example but I get the error (below) when running this MATLAB script:
stk = stkApp.Personality2;
stkScenario = stk.CurrentScenario;
if isempty(stkScenario)
error('Please load a scenario');
end
facility = stk.GetObjectFromPath('Facility/RRFac');
satellite = stk.GetObjectFromPath('Satellite/P02S01');
access = satellite.GetAccessToObject(facility);
access.ComputeAccess;
accessDP = access.DataProviders.Item('Access Data').Exec(stkScenario.StartTime,stkScenario.StopTime);
accessStartTimes = accessDP.DataSets.GetDataSetByName('Start Time').GetValues;
accessStopTimes = accessDP.DataSets.GetDataSetByName('Stop Time').GetValues;
accessIntervals = access.ComputedAccessIntervalTimes;
accessDataProvider = access.DataProviders.Item('Access Data');
dataProviderElements = {'Start Time';'Stop Time'};
accessIntervals = access.ComputedAccessIntervalTimes;
for i = 1:1:accessIntervals.Count
[start, stop] = accessIntervals.GetInterval(i-1);
satelliteDP = satellite.DataProviders.Item('DeckAccess Data').Group.Item('Start Time LocalHorizontal Geometry').ExecElements(accessStartTimes{1},accessStopTimes{1},{'Time';'Range Rate'});
satelliteAlt = satelliteDP.DataSets.GetDataSetByName('Range Rate').GetValues;
end
Error using Interface.AGI_STK_Objects_12_IAgDrDataSetCollection/GetDataSetByName Invoke Error, Dispatch Exception: The parameter is incorrect.
Error in GenRRreport (line 37)
satelliteAlt = satelliteDP.DataSets.GetDataSetByName('Range Rate').GetValues
Why does it throw this error and how to avoid that?
So, I want to make if a purchase is completed, a GUI will appear
#x72 is already answered it but I don't use her codes. I tried to use another code but didn't work either.
I didn't get it work but no error messages
Here's the code:
Script:
-- Products for purchases
local Donate1ProductID = 1296480045
local Donate2ProductID = 1296484976
local Donate3ProductID = 1296484935
local Donate4ProductID = 1296484932
local Donate5ProductID = 1296483595
local Donate6ProductID = 1296484931
local Donate7ProductID = 1296484933
-- Required depencies/services
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseSuccess = ReplicatedStorage.PurchaseSuccess
-- Code
MarketplaceService.ProcessReceipt = function(receiptInfo)
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if receiptInfo.ProductId == Donate1ProductID or Donate2ProductID or then Donate3ProductID or Donate4ProductID or Donate5ProductID or Donate6ProductID or Donate7ProductID then
PurchaseSuccess:FireAllClients()
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
LocalScript:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local PurchaseSuccess = ReplicatedStorage:WaitForChild("PurchaseSuccess")
local playerGui = player:WaitForChild("PlayerGui")
local Main = playerGui:WaitForChild("AntiPiracyCheck")
PurchaseSuccess.OnClientEvent:Connect(function ()
Main.PurchaseSuccess:TweenPosition(
UDim2.new(0.258, 0,0.279, 0),
Enum.EasingDirection.InOut,
Enum.EasingStyle.Quad,
1,
true
)
end)
What is the problem anyway?
You have a syntax error in your server Script's if-statement, that might be why nothing is happening. You have a stray then in the middle of the condition list, and it should only be at the end.
When using an if-statement to check whether a condition is true, you need to be explicit.
if receiptInfo.ProductId == Donate1ProductID or Donate2ProductID or Donate3ProductID or Donate4ProductID or Donate5ProductID or Donate6ProductID or Donate7ProductID then
This if-statement is saying, "if the receipt's productId matches Donate1ProductID, do a thing. Or if Donate2ProductID exists, do a thing. Or if Donate3ProductID exists, do a thing..." You aren't asking if the receipt's productID matches any of the listed product ids, you are only asking if the other product ids exist. You need to explicitly ask if receiptInfo.ProductID == [THE_PRODUCT_ID] in each case...
if receiptInfo.ProductId == Donate1ProductID or
receiptInfo.ProductId == Donate2ProductID or
receiptInfo.ProductId == Donate3ProductID or
receiptInfo.ProductId == Donate4ProductID or
receiptInfo.ProductId == Donate5ProductID or
receiptInfo.ProductId == Donate6ProductID or
receiptInfo.ProductId == Donate7ProductID then
But there is a nicer way to do that, you can put all of the ids as the keys of a table and check if the receipt matches any of the known keys. Try something like this :
-- Products for purchases mapped to a message about it.
local donationIDMap = {
[1296480045] = "Donated X amount",
[1296484976] = "Donated X amount",
[1296484935] = "Donated X amount",
[1296484932] = "Donated X amount",
[1296483595] = "Donated X amount",
[1296484931] = "Donated X amount",
[1296484933] = "Donated X amount",
}
-- Required depencies/services
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseSuccess = ReplicatedStorage.PurchaseSuccess
-- Code
MarketplaceService.ProcessReceipt = function(receiptInfo)
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
local donationMessage = donationIDMap[receiptInfo.ProductId]
local messageExists = donationMessage ~= nil
if messageExists then
-- you could send the message to the client so they know that it worked..
PurchaseSuccess:FireClient(player, donationMessage)
return Enum.ProductPurchaseDecision.PurchaseGranted
end
-- not sure what they bought.
local message = "NO HANDLER SPECIFIED FOR PRODUCT ID : %d. %s DID NOT RECEIVE ANYTHING FOR THIS PURCHASE.":format(receiptInfo.ProductId, player.Name)
warn(message)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
Then on the client you can optionally handle the message from the server like this...
PurchaseSuccess.OnClientEvent:Connect(function(donationMessage)
-- put the message into a textbox...
-- local txtBox = Main.Purchased.Message -- find the path to some ui element
-- txtBox.Text = donationMessage
The problem is, Your LocalScript was in REPLICATEDSTORAGE.
I'm using a Lua class to create two objects, each of which must check where the other is to determine their movement. I'm attempting to use timer.performWithDelay to have them check every second, but for some reason when I try to do this, the line
o.moveCheckTimer = timer.performWithDelay(1000, o:moveCheck, 0)
in the class constructor throws an error stating that "Function arguments are expected near ','".
I have attempted to use an anonymous function like this:
o.moveCheckTimer = timer.performWithDelay(1000, function() o:moveCheck() end, 0)
but that causes the timer of both objects to only call the function for the most recent object that was created and not for itself (also very confusing behavior, and if anyone knows why this happens I would love to learn why).
I have dug through the API and info on method calls thoroughly, but I can't seem to find anything that uses them both together, and I feel like I'm missing something.
How can I use the method call as the listener for this timer?
Here is the full constructor:
Melee = {}
Melee.__index = Melee
function Melee:new(id, name, lane, side)
local o = {}
setmetatable(o, Melee)
o.id = id
o.name = name
o.lane = lane
o.side = side
if name == "spearman" then
o.maxhp = 100
o.range = 1
o.damage = {10, 20}
o.imageName = "images/rebels/spearman.png"
else
error("Attempted to create melee unit with undefined name")
end
o.hp = o.maxhp
--Display self
o.image = display.newImageRect(mainGroup, "images/rebels/spearman.png", 80, 80)
o.image.x = 0
o.image.y = lanes[lane]
o.image.anchorY = 1
if side == 2 then
o.image.xScale = -1
o.image:setFillColor(0.8)
o.image.x = display.contentWidth - 100
end
--Move and attack
local destination = display.contentWidth
if side == 2 then
destination = 0
end
o.moving = 1
o.movement = transition.to(o.image, {x = destination, time = 30000+math.random(-200,200)})
o.moveCheckTimer = timer.performWithDelay(1000, o:moveCheck, 0)
--o.attackTimer = timer.performWithDelay(1000, self:attack, 0)
return o
end
I'm new with corona/lua and i'm i can't find a solution to this thing. I'm trying to spawn a object that fall from top to down and should stop at the bottom of the screen. Then i'll create the touch event etc etc..
but for now the problem is that i recieve this error:
attempt to index global 'physics' (a nil value)
and objects ofc doesn't fall down.
here is my code:
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
local buttonY = display.contentWidth * 0.02
local buttonWidth = display.contentWidth * 0.1
local buttonHeight = display.contentWidth * 0.1
background = display.newImage("graphics/background.jpg")
local localGroup = display.newGroup()
local spawnTable = {}
function spawnLattina(params)
local object = display.newImage(params.image, params.buttonX,50);
object.objTable = params.objTable;
object.index = #object.objTable+1;
object.name = "object:".. object.index;
--fisica
if params.hasBody then
object.density = params.density or 0;
object.friction = params.friction or 0;
object.bounce = params.bounce or 0;
object.isSensor = params.isSensor or false;
object.bodyType = params.bodyType or "dynamic";
print(object.density .. " Friction: ".. object.friction .."bodyType: "..object.bodyType)
physics.addBody(object, object.bodyType,
{density = object.density,
friction = object.friction,
bounce = object.bounce}
)
end
object.group = params.group or nil
object.group:insert(object)
object.objTable[object.index] = object
return object
end
for i = 1, 2 do
local spawns = spawnLattina(
{
image = "graphics/lattina.png",
objTable = spawnTable,
buttonX = math.random(50,480),
hasBody = true,
density = 0,
friction = 12,
bodyType = "static",
group = localGroup,
}
)
end
You haven't started the physics engine. Write the following lines on the top of your class:
local physics = require "physics"
physics.start()
Keep Coding.................. :)
I have created a lua file called moonclass.lua
which created a display object of a moon
i have created two instances of this class, aka two moons,
these were created in level1.lua
however when i try to rotate both moons, only the last moon that is created will rotate, regardless of which one i refer the rotation to.
moonclasss.lua below:
local moon = {}
local moon_mt = { __index = moon } -- metatable
local sprite
function moon.new(name) -- constructor
local newMoon = {
name = name or "unnamed",
}
return setmetatable( newMoon, moon_mt)
end
function moon:draw(x,y,group)
sprite = display.newImage("image/moon.png")
sprite.x = x
sprite.y = y
physics.addBody( sprite, "dynamic", { density=1, friction=0.8, bounce=0.05, radius=145 } )
group:insert (sprite)
end
function moon:talk() print( self.name .. " is called." )end
function moon:y(y)
sprite.y = sprite.y + y
return y
end
function moon:x(x)
sprite.x = sprite.x + x
return x
end
function moon:rotate(r)
sprite.rotation = sprite.rotation + r
return sprite
end
return moon
and level1.lua:
local storyboard = require ("storyboard")
local scene = storyboard.newScene()
storyboard.purgeScene ("menu")
display.setStatusBar( display.HiddenStatusBar )
local physics = require("physics")
physics.start()
physics.setGravity(0, 0)
function scene:createScene(event)
local group = self.view
local background = display.newGroup()
local foreground = display.newGroup()
group:insert(background)
group:insert(foreground)
--BACKGROUND
backgroundimage = display.newImage("image/galaxy background.png")
foreground:insert(backgroundimage)
backgroundimage.x = display.contentWidth/2
backgroundimage.y = display.contentWidth/-1
require "starclass"
require "controls"
local moon = require ("moonclass")
local moon1 = moon.new("moon1")
moon1:draw(0,400,foreground)
local moon2 = moon.new("moon2")
moon2:draw(300,400,foreground)
moon1:talk()
moon2:talk()
local platform = require "platformclass"
platform.draw(512,200,foreground)
local platform2 = require "platformclass"
platform2.draw(50,0,foreground)
local rocket = require "rocketclass"
rocket.draw(200,600,foreground)
function gameloop(event)
--if (rockety > -1920 and rockety < 320) then
--foreground.y = rockety + 320
--end
moon2:rotate(1)
moon1:rotate(-2)
end
Runtime:addEventListener("enterFrame", gameloop)
end
-- ENTER SCENE --
function scene:enterScene (event)
--local chapter1level1 = require("scene objects").loadlevelenterscene()
end
-- EXIT SCENE --
function scene.exitScene(event)
controlthrust:removeEventListener( "touch", thrust)
controlleft:removeEventListener( "touch", rotateleft)
controlright:removeEventListener( "touch", rotateright)
Runtime:removeEventListener("enterFrame", gameloop)
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
return scene
local moon = require("moonclass")
local moon2 = require ("moonclass")
These lines return the same object.
require does not load a module twice.
Second require simply returns the value that was returned by the first require.
You must define a constructor function and call it to create new object.
EDIT :
local moon = {}
local moon_mt = { __index = moon } -- metatable
function moon.new(name) -- constructor
local newMoon = {
name = name or "unnamed"
}
return setmetatable( newMoon, moon_mt)
end
function moon:draw(x,y,group)
self.sprite = display.newImage("image/moon.png")
self.sprite.x = x
self.sprite.y = y
physics.addBody( self.sprite, "dynamic", { density=1, friction=0.8, bounce=0.05, radius=145 } )
group:insert (self.sprite)
end
function moon:talk() print( self.name .. " is called." )end
function moon:y(y)
self.sprite.y = self.sprite.y + y
return y
end
function moon:x(x)
self.sprite.x = self.sprite.x + x
return x
end
function moon:rotate(r)
self.sprite.rotation = self.sprite.rotation + r
return self.sprite
end
return moon