How can I pass a global variable from ServerScript to LocalScript? Can someone also provide an example I can follow? Many Thanks!
✓ ANSWER ACCEPTED
How can I pass a global variable from ServerScript to LocalScript?
1 answer
✓ Accepted answer
You could make a RemoteEvent about this, but you could figure out the rest yourself since it isnt that hard
lets say there is a RemoteEvent in replicatedstorage right now, named TestRemote
this is a serverscript, in serverscriptservice for example
game:GetService("Players").PlayerAdded:Connect(function(player)
local x = 0
x = 1
x = 2
x = 3
x = 4
x = 5
game:GetService("ReplicatedStorage").TestRemote:FireClient(player, x)
x = 6
x = 7
end)
and here is a localscript:
game:GetService("ReplicatedStorage").TestRemote.OnClientEvent:Connect(function(x) -- Warning, do not put player parameter here, it wont work because there is no player parameter in OnClientEvent, i made that mistake like 3 times lol
print(x) -- 5
end)
Of course, you can transfer Instances too,
script:
game:GetService("Players").PlayerAdded:Connect(function(player)
game:GetService("ReplicatedStorage").TestRemote:FireClient(player, game.Workspace.Part)
end)
localscript:
game:GetService("ReplicatedStorage").TestRemote.OnClientEvent:Connect(function(part)
print(part.Name) -- Part
end)
oh by the way, if your question isnt like what i answered, or you can explain a little more details on the script (you can delete some parts you dont want to show, some people do it)
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
thanks, i'll try it tm. Does this also work if I want to pass variables from one serverscript to another?
If you want to share variables between scripts, I'd recommend using Modules
But if you want to transfer data from server to client, then use RemoteEvents