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

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

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.

4 comments

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

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.

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.

0

What script doesnt work?

1 answer

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.

1 comment

0

Yes, it works fine in the console but can you tell me how to make it work for the client?

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