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

How Can I Share Variables With Remote Events?

Asked by 6 years ago
Edited 6 years ago

Okay, I'm still learning a lot so please dont be mad. So im trying to pass a variable from a local script with a remote event to a server script. Heres an example:

--local script:
script.Parent.MouseButton1Down:Connect(function() -- this is a example so this might seem choppy
local selected = "Gun1" --As an Example
game.ReplicatedStorage.GetGun:FireServer(player,selected)
end)

--Server Script:
function getgun(player,selected)
gun = game.ServerStorage.Weapons:FindFirstChild(selected)
gun.Parent = player.Backpack 
end 
game.ReplicatedStorage.GetGun.OnServerEvent:Connect(getgun)

Both 'player' and 'selected' are considered as the localplayer. Im trying to make 'selected' as the variable im trying to pass to my server script but its not working.

Any Help?

1
You don't FireServer with the player as the first argument. Roblox adds that automatically in for the server event. 2eggnog 981 — 6y

1 answer

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

The function :FireServer() does not require a player argument unlike the :FireClient() function which does, therefore the corresponding event .OnServerEvent does return a player in the first argument, after this would be any thing you sent over as well. So, first let's fix your local script

--local script:
script.Parent.MouseButton1Down:Connect(function() 
local selected = "Gun1"
game.ReplicatedStorage.GetGun:FireServer(selected) --I am not including a player argument
end)

Now for the script

--Server Script:
function getgun(player,selected) -- OnServerEvent returns player first argument
gun = game.ServerStorage.Weapons:FindFirstChild(selected)
gun.Parent = player.Backpack 
end 
game.ReplicatedStorage.GetGun.OnServerEvent:Connect(getgun)

Vise versa the function :FireClient() does require a player argument and the event .OnClientEvent does not return a player

Ad

Answer this question