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

Issue with remote events. Player is both values?

Asked by 4 years ago

I have a basic inventory system which works fine, but I want it that when the player clicks the item, it equips it. My only problem is that when I fire the remote event, it doesn't work. As the value, which is script.parent.text goes to the player?

Local script:

script.Parent.MouseButton1Click:Connect(function()

    local player = game.Players.LocalPlayer

    game.ReplicatedStorage.KnifeChange:FireServer(player, script.Parent.Text)

end)

Server Script:

local function knifeChange(player,value)

    game.ReplicatedStorage.CurrentKnife.Value = value

end

game.ReplicatedStorage.KnifeChange.OnServerEvent:Connect(knifeChange)

2 answers

Log in to vote
0
Answered by 4 years ago

The player on :FireServer() is already sent as a parameter, so all you have to do is send the extra arguments.

LocalScript, and for the .OnServerEvent() the first parameter passed through is the player, so it will be...

RemoteEvent.OnServerEvent:Connect(function(player, extraArguments)
    print(player.Name)
end)

So, in the LocalScript you don't have to pass through the player, but you will have to pass through the other arguments. In the ServerScript you will have to identify the player as the first parameter passed through.

So, this is how your script should look like.

LocalScript

local remote = game:GetService("ReplicatedStorage"):WaitForChild("KnifeChange")

script.Parent.MouseButton1Click:Connect(function()
    local knifeName = script.Parent

    remote:FireServer(knifeName.Text)
end)

ServerScript

local remote = game:GetService("ReplicatedStorage"):WaitForChild("KnifeChange")
local currentKnife = game:GetService("ReplicatedStorage"):WaitForChild("CurrentKnife")

remote.OnServerEvent:Connect(function(player,knife)
    currentKnife.Value = knife
end)

Common Mistakes

  • Make sure you are using a StringValue to change the name

  • Make sure you are using the correct directories, names, etc.

Hope this helped! Feel free to select this as an answer if this worked for you!

0
Thank you! Worked exactly how I wanted it! Quantrium 14 — 4y
0
No problem! Glad to help! killerbrenden 1537 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

The player is already automatically sent as a parameter.

Try this in the local script:

script.Parent.MouseButton1Click:Connect(function()

    game.ReplicatedStorage.KnifeChange:FireServer(script.Parent.Text)

end)
0
Didn't work, came up with: "bad argument #3 (string expected, got Object)" as an error message. Quantrium 14 — 4y
0
try game.ReplicatedStorage.KnifeChange:FireServer(tostring(script.Parent.Text)) ryanaskew 50 — 4y
0
Same error. Quantrium 14 — 4y
0
print(script.Parent.Text) - same error? ryanaskew 50 — 4y
0
try game.ReplicatedStorage.KnifeChange:FireServer(script.Parent.Name) ryanaskew 50 — 4y

Answer this question