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

How do I add to my leaderstats value?

I'm currently making a game where you have to find presents. I want to make it so that when you touch a present it changes to green and adds one point to your leaderstats value. I also want to make sure that if you touch a present that has already been touched it wont give you another point. In one script under the first present I've done this:

script.parent.Touched:Connect(function(hit)
	game.Workspace.Presents.Present1.Mainpart.Color = Color3.new(0.0730297, 0.630625, 0)
	
	game.Players.PlayerAdded:Connect(function(player)
		local leaderstats = Instance.new("Folder",player)
		leaderstats.Name = "leaderstats"

		local Presents = Instance.new("IntValue", leaderstats)
		Presents.Name = "Presents"
		
		Presents = Presents * 1
	end)
	
end)

I also have another script under server script service that is making the leaderstats:

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

I hope this makes sense

1 answer

✓ Accepted answer

You can use the parameter that comes with the Touched event to find the character that came into contact with this part.

To make it so the player can only receive the point once, you can use a table to track the players that have made contact with the part and use an if statement to check whether they are in the table.

local Players = game:GetService("Players")
local PlayersTable = {}

script.Parent.Touched:Connect(function(hit)

	if Players:GetPlayerFromCharacter(hit.Parent) then
		local Player = Players:GetPlayerFromCharacter(hit.Parent)
		local leaderstats = Player:WaitForChild("leaderstats")
		local Presents = leaderstats:WaitForChild("Presents")

		if not table.find(PlayersTable, Player.Name) then
			table.insert(PlayersTable, Player.Name)
			game.Workspace.Presents.Present1.Mainpart.Color = Color3.new(0.0730297, 0.630625, 0)
			Presents.Value += 1
		end
	end
end)

1 comment

0

Thank you so much

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