Adding +1 to leaderboard when walk through part

I am new to scripting and I wanted to make a small system where you gain a point on the leaderboard every time you walk through a part. However, I am having trouble getting it to work. Here is the script:

local Part = game.Workspace:FindFirstChild("AddPoint")


-- Make the leaderboard
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	-- Name the leaderboard
	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Parent = leaderstats
end)

-- Give +1 point when player walks through "AddPoint"
Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.leaderstats.Points.Value += 1
	end
end)```

hit.parent references to the Player’s character, not the Player’s instance. Try:
game.Players:GetPlayerFromCharacter(hit.Parent):FindFirstChild(“leaderstats”).Points.Value += 1

2 Likes

As @HelpSupport01 has said, you can only detect if a player has touched a part by doing part.Touched which returns the parameters of the hit part. You can only access the player through the hit’s parent which is only the character:

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local leaderstats = player.leaderstats
leaderstats.Points.Value += 1

Hope this helps! :slight_smile:

1 Like

the player is actually located in the players service, and the leaderboard is located inside that player, not its character, i would also add a cooldown for players to not get too many points in a small period of time. here’s the modified code:

local Part = game.Workspace:FindFirstChild("AddPoint")

local COOLDOWN = 1 --change it to any number you would like

-- Make the leaderboard
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	-- Name the leaderboard
	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Parent = leaderstats
end)

-- Give +1 point when player walks through "AddPoint"
Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- searches for the player
		plr.leaderstats.Points.Value += 1 --adds the points to the intValue
		wait(COOLDOWN) --waits for the cooldown to end
	end
end)

Hi, this manages to add a point to the leaderboard but as all the body parts are going through the AddPoint part it still adds a whole bunch of points despite the cooldown. What would be the solution?

Thanks for your help!

I tested the script and i see that it does indeed add a ton of points at once, i will be right back with the solution in a moment

Hi! I managed to fix it by adding a debounce. Not sure if this is the best method though, and would love to see what you’ve made.

local Part = game.Workspace:FindFirstChild("AddPoint")

-- Cooldown timer
local cooldown = 2
local debounce = false

-- Make the leaderboard
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	-- Name the leaderboard
	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Parent = leaderstats
end)

-- Give +1 point when player walks through "AddPoint"
Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if not debounce then
			debounce = true
			
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- searches for the player
			plr.leaderstats.Points.Value += 1 --adds the points to the intValue
			
			task.wait(cooldown)
			debounce = false
		end
	end
end)```
local Part = game.Workspace:FindFirstChild("AddPoint")
local COOLDOWN_IS_ON = false
local COOLDOWN = 1 --change it to any number you would like

-- Make the leaderboard
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	-- Name the leaderboard
	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Parent = leaderstats
end)

-- Give +1 point when player walks through "AddPoint"
Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if COOLDOWN_IS_ON == false then
			COOLDOWN_IS_ON = true
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			plr.leaderstats.Points.Value += 1
			wait(COOLDOWN)
			COOLDOWN_IS_ON = false
		end
	end
end)

here you go :wink:

haha, we had the same solution as seen in my upper post

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.