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

How can I pass a global variable from ServerScript to LocalScript?

Asked by 3 years ago
Edited 3 years ago

How can I pass a global variable from ServerScript to LocalScript? Can someone also provide an example I can follow? Many Thanks!

1 answer

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago

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)

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

Answer this question