Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Issue with remote events. Player is both values?

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

✓ Accepted answer

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!

2 comments

0

Thank you! Worked exactly how I wanted it!

0

No problem! Glad to help!

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)

5 comments

0

Didn't work, came up with: "bad argument #3 (string expected, got Object)" as an error message.

0

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

0

Same error.

0

print(script.Parent.Text) - same error?

0

try game.ReplicatedStorage.KnifeChange:FireServer(script.Parent.Name)

This discussion is preserved as it was. New answers, comments, and votes are closed.