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 points to someone when they touch a part?

This is my leaderboard code and I am trying to figure out a way to have points given to someone who is touching a part.

local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("MoneyStats")

game.Players.PlayerAdded:Connect(function(Player)

local Leaderstats = Instance.new("Folder")

Leaderstats.Name = "leaderstats"

Leaderstats.Parent = Player

local Points = Instance.new("IntValue")

Points.Name = "Points"

Points.Value = DataStore:GetAsync(Player.UserId) or "0"

Points.Parent = Leaderstats

end)

game.Players.PlayerRemoving:Connect(function(Player)

DataStore:SetAsync(Player.UserId, Player.leaderstats.Points.Value)

end)

2 comments

0

You have something at the end of Leaderstats.Parent = Player. Just want to make sure that that's not a part of the Script.

0

Yeah. it's not.

1 answer

✓ Accepted answer

Hi, MrbaconFTW

To start off with your leaderstats script is broken. I have fixed it:

local Name = "Points"
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats")

game.Players.PlayerAdded:Connect(function(Player)
	
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	
	local Value = Instance.new("IntValue")
	Value.Name = Name
	Value.Parent = Leaderstats

end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, Player.leaderstats[Name].Value)

end)

As you can see I have changed the name of the IntValue on lines 11, 12 and 13 to value instead of "Points".

Now adding points when a player hits a part is actually quite simple. Insert this into the part:

debounce = false --Debounce makes it so the "Touched" function can't be spammed.

script.Parent.Touched:Connect(function(hit) 
	if not debounce then debounce = true
	       local player = game.Players:GetPlayerFromCharacter(hit.Parent);--Getting Player.
	       If player then
		       player.leaderstats.Points.Value = player.leaderstats.Points.Value + 1 --Adding a number to "Points". 
	       end
	       wait(1) --The cooldown before the player can get another point. (Debounce)
	       debounce = false 
		
	end
end)

Hopefully this helps.

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