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

How could you make values but theyre different for each player?

Just a heads up: this isnt for a current script, just want to know for later projects.

So for say I had an IntValue but it would need to be different for each player. This might be the script

local coins = (intvalue)

workspace:WaitForChild("Part").ClickDetector.MouseClick:Connect(function()

	coins.Value = coins.Value + 1

end)

Just something simple, hope to get some responses!

3 comments

0

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

0

So if i edit an intvalue in a localscript, itll be different for each player?

0

Yes, but you cant really access other player's values this way

1 answer

✓ 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!

1 comment

0

I guess that makes sense. Should've guessed considering how leaderstats works lol

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