Here's my scenario: each player has 500 health and 500 "shield" which is essentially extra health. I've assigned shield to an IntValue inside a GUI and I want shield to be depleted before health. I created a RemoteFunction for defining damage to players and I also have a brick that is supposed to damage players when touched.
Here is the LocalScript (inserted inside a ScreenGUI):
--Remote functions local damageInfantry = game.ReplicatedStorage.DamageInfantry function damageInfantry.OnClientInvoke(player, humanoid, amount) if shield.Value > 0 then shield.Value = shield.Value - amount if shield.Value < 0 then --More damage done than shield remaining local difference = shield.Value * (-1) humanoid:TakeDamage(difference) end else humanoid:TakeDamage(amount) end end
Here is the server script (inserted inside the brick):
--Functions function onTouch(part) local character = part.Parent local humanoid = character:FindFirstChild("Humanoid") if humanoid then if canDamage then damageInfantry:InvokeClient(humanoid, 100) canDamage = false wait(1) canDamage = true end end end
This gives me an error: attempt to perform arithmetic on local 'amount' (a nil value) What am I doing wrong and is there a better way to handle damaging humanoids with this "shield" mechanic?
Thanks in advance.
player
parameter. Pointed out by climethestair. Now, on InvokeClient
, that is where you need a player argument.damageInfantry:InvokeClient(player, blah your parameters go here)
Players.PlayerAdded
, ClickDetector.MouseClick
, etc. This is the client we are invoking the RemoteFunction
to.OnClientInvoke
, it is required and the first argument to be passed to InvokeClient
.Players.LocalPlayer
to get the player from LocalScript
s. Any event that passes the Player
as a parameter (e.g. Dialog.DialogChoiceSelected
), is server side only. If you passed the player on OnClientInvoke
, your InvokeClient
would have to look like this:damageInfantry:InvokeClient(player, player, blah other parameters here)
player
parameter.function damageInfantry.OnClientInvoke(player, humanoid, amount)
You have an extra argument here, it should only be humanoid, amount. Usually, the event only takes the player argument when it's from client to server so that the server knows who sent it.
function damageInfantry.OnClientInvoke(humanoid, amount)