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

Why can't I use _G for adding points to player's leaderstats?

Asked by 4 years ago

So, I've been experimenting with _G and I think it's broken for a reason: when I first tried to print some object's name, it printed it; I don't know why but it doesn't print that anymore but instead prints 'nil'. I also can't understand why it doesn't work for adding points to a player's leaderstats. Here's my code in the first script:

game.Players.PlayerAdded:Connect(function(player)
    local LS = Instance.new("Folder", player)
    LS.Name = "leaderstats"

    local T = Instance.new("IntValue", LS)
    T.Name = "Test"

    _G.Test = T.Value
end)

Here's the second:

script.Parent.MouseClick:Connect(function()
    _G.Test = _G.Test + 25
end)

They both are in different locations: the first one is in the ServerScriptService, the second is in a part in the workspace. Please explain why won't it work.

0
This script appears to work fine for me, also, I'm not too sure if you really even need a global value, you could just read from the leaderstat moo1210 587 — 4y
0
I know I could, but when I do so, nothing happens and I'm trying to use something like this. If it works fine for you, I will try again. DangerousKillerTimur 54 — 4y
0
Are you trying to set the leaderstat to the global value? That isn't in the script is thats the case, also my setup was the same I put the 1st script in serverscriptservice and made two parts, one to print the global value and one to add, and I was able to confirm it did add to the global value. moo1210 587 — 4y
0
What script doesnt work? JesseSong 3916 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

The problem is that the ServerScriptService won't replicate over to clients, it is exclusively server-side. However, this is only a problem for the clients; if you switch to the server's view everything should be correct. Try moving the first script into Workspace, rather than ServerScriptService.

0
Yes, it works fine in the console but can you tell me how to make it work for the client? DangerousKillerTimur 54 — 4y
Ad

Answer this question