✓ Accepted answer
You can store values within the Player object. Here's an example.
I will break this down line by line.
game:GetService("Players").PlayerAdded:Connect(function(plr)
local intValue = Instance.new("IntValue")
intValue.Name = "Coins"
intValue.Parent = plr
end)
workspace.Part.ClickDetector.MouseClick:Connect(function(plr)
local coins = plr:FindFirstChild("Coins")
if coins then
coins.Value = coins.Value + 1
end
end)
The game:GetService("Players").PlayerAdded:Connect(function(plr) first gets the Player service, which has helpful events like PlayerAdded. We then call the "Connect" function to connect to the event and ask for the first variable (which will be the player) to be defined as "plr".
Next, we create a new IntValue and define it to the intValue variable. We rename it to "Coins" and parent it to the plr variable, which holds the player.
Next, we access the ClickDetector in the part (you don't need WaitForChild) by indexing it. We then access the ClickDetector event and connect to it. We ask for the first variable (the player) to be defined as "plr" like the event above.
We check to see if the player does indeed have an IntValue called coins by calling FindFirstChild on the player. If "Coins" is not found, FindFirstChild will return nil. The if statement checks to see if coins is nil. If it does not, it increases the value of coins by one.
Hope this helps!
3 comments
If you use a LocalScript, it will only run on the client, so it's a different value for every player. The second option is to put a NumberValue inside the player's backpack or character, and the you change or read that through the script
So if i edit an intvalue in a localscript, itll be different for each player?
Yes, but you cant really access other player's values this way