Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to use RemoteFunctions?

Asked by 6 years ago

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.

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

This is because you added a 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)

You’ll have to find the player by using Players.PlayerAdded, ClickDetector.MouseClick, etc. This is the client we are invoking the RemoteFunction to.

Although not a parameter in OnClientInvoke, it is required and the first argument to be passed to InvokeClient.

Use Players.LocalPlayer to get the player from LocalScripts. 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)

You’d have to add an extra player parameter.

0
I suspected I might've messed that up, I'm pretty new to Lua lol. But then how would you suggest I define player on the server script? Is Players.PlayerAdded the best option? Perthrosama 24 — 6y
0
Well, there are lots of ways. You can use player in touched event (you have to use GetPlayerFromCharacter). It’s up to you, just get the player.  User#19524 175 — 6y
0
Thank you so much! Everything works great now. Perthrosama 24 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
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)
0
Thanks for clearing that up, I wasn't sure exactly how the player argument was handled. But I still get the same error message unfortunately. amount == nil Perthrosama 24 — 6y

Answer this question