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

Sharing player variables through REs doesn't work. Help?

I'm making a shop system for my game and I need a lot of help with this whole remote event thing. I'm telling the server that a player has bought the item and the server needs to give it to the player.

local replicate = game:GetService("ReplicatedStorage")

script.Parent.MouseButton1Up:Connect(function(player)
	replicate.speed:FireServer(player)
end)

^^ localscript

game:GetService("ReplicatedStorage").speed.OnServerEvent:Connect(function(player)
	print(player.." has bought the thing")
end)

^^ serverscript (print is just there for debug)

The serverscript isn't picking up any player from the localscript.

3 comments

0

i can't tell because of how small the context is, but is the first script which fires the remote event a server script or a localscript?

0

i should also mention that you don't have to send "player" when firing to the server, it will send it without your consent as a way of security. it would be ridiculous if we could tamper with that.

0

also if this is server-server communication then use a bindable event or something

1 answer

✓ Accepted answer

The issue isn't the remote event itself, it's your usage of MouseButton1Up. The player who clicked whatever GuiButton is not the parameter of the event. Instead, the parameters are as follows:

GuiButton.MouseButton1Up:Connect(function(x,y)
	--where x represents the mouse's x position on the screen, and y represents its y position
end)

In fact, you don't even need to get the player that way. Since you're using a LocalScript, you can simply access the LocalPlayer. The truth is, though, that you don't need the LocalPlayer at all. :FireServer() has no required arguments! You may send variable information through this method if necessary, but the player is not required. This is because the server will recognize automatically what client fired the event. As such, OnServerEvent has a default parameter of player, being the player who fired the event.

--Local Script

local replicate = game:GetService("ReplicatedStorage")

script.Parent.MouseButton1Up:Connect(function() --you don't have to enter the parameters if you won't use them
    replicate.speed:FireServer() 
end)
--Server Script

game:GetService("ReplicatedStorage").speed.OnServerEvent:Connect(function(player)
    print(player.Name .. " has bought the thing")
end)

Again, note the fact that player being a parameter of OnServerEvent has nothing to do with the fact that you were sending it through the remote event previously. It is simply the defaut parameter.

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