How do i add 1 point to a player every second when touching a part

i want to make kinda a capturing system where each second a player is touching the capturing part it gives the player 1 point. i actually thought i solved this with a localscript but i realized when adding leaderstats with a local script the rest of the game can see how much points the player has. this means i have to do this with a normal script.

Script:

script.Parent.Touched:Connect(function(hit)
	local user = game.Players:GetPlayerFromCharacter(hit.Parent)
	if user ~= nil then
		user.leaderstats.Capturing.Value = 1
		wait(0.05)
		user.leaderstats.Capturing.Value = 1
	end
end)

script.Parent.Touched:Connect(function(P)
	local user = game.Players:GetPlayerFromCharacter(P.Parent)
	if user ~= nil then
		while wait(1) do
			if user.leaderstats.Capturing.Value == 1 then
				user.leaderstats.Points.Value += 1
			end
		end
	end
end)

You can tell by the script that i was trying to make this system with Connect functions but this doesnt work because it activates a ton of them at once and not once every second.

I think the only way to solve this is by making a function that can only run once every second while the part is being touched but i dont know how to do that.

Thanks to everyone that can help with this

3 Likes
script.Parent.Touched:Connect(function(P)
	local touching = true
	local user = game.Players:GetPlayerFromCharacter(P.Parent)
	if user ~= nil then
		while touching do
			wait(1)
			user.leaderstats.Points.Value += 1
			
		end
	end
end)

the same thing happens as it registers multiple times making it run that loop 20 times at once

sorry im also a beginner and i didnt test this with leaderstats, so does it add multiple points into the leaderstat in one second? Just trying to see where its going wrong

1 Like

yea and the gain of points exponentially increases the more you touch it

1 Like

exponentially??? thats very weird

also i dont think we need a second touching function

wrong word for it i meant like it increases the more you touch it

that was for my local script that i used to have but that doesnt really matter now

oh yeah thats right thats because touching is set to true the first time it gets touched it never gets set to false after stepping off, so it would increase without the part being touched. Im tryna figure out how to solve that

You could try something like this.

local GettingPoints = false
script.Parent.Touched:Connect(function(hit)
    if GettingPoints then
        return
    end
	local user = game.Players:GetPlayerFromCharacter(hit.Parent)
	if user then --// if user has the same effect as if user ~= nil
        GettingPoints = true
		user.leaderstats.Points.Value += 1
        task.wait(1)
        GettingPoints = false
	end
end)

If I understood your script correctly, then this should work. The idea here is the variable GettingPoints will prevent the function from running if it is true, as it will just return instead of running the function, and it will be set to true as soon as you start getting points, and set to false once the wait is over.
Hope this helps :happy1:

it only adds one point each time i touch it and doesnt loop

Oh, okay, if you want it to loop, something like this might work (I hope):

local Touching = false

script.Parent.Touched:Connect(function(hit)
	if Touching then
		return
	end
	local user = game.Players:GetPlayerFromCharacter(hit.Parent)
	if user then 
		Touching = true
		while Touching do
			user.leaderstats.Points.Value += 1
			task.wait(1)
		end
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	local user = game.Players:GetPlayerFromCharacter(hit.Parent)
	if user then
		Touching = false
	end
end)

it works the first time i touch it but when i get off it and get back on it 75% of the time it adds multiple every second and 25% of the time it acts normal, Also when a player equips an item it gives the player 100s of points

Try this:

local part = script.Parent

local cooldown = false

part.Touched:Connect(function(hit)
    local character = hit.Parent
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    local user = game.Players:GetPlayerFromCharacter(hit.Parent)

    if humanoid and not cooldown then
        user.leaderstats.Points.Value += 1
        cooldown = true
        task.wait(1)
        cooldown = false
    end
end)

it only adds 1 every time i touch it

I don’t know why it would give the player 100s of points, but if its because its touching the part very rapidly, then adding a new variable like this may work?

local Touching = false
local First = 0

script.Parent.Touched:Connect(function(hit)
	if Touching then
		return
	end
	local user = game.Players:GetPlayerFromCharacter(hit.Parent)
	First += 1
	if user and First == 1 then
		Touching = true
		while Touching do
			user.leaderstats.Points.Value += 1
			task.wait(1)
		end
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	local user = game.Players:GetPlayerFromCharacter(hit.Parent)
	if user then
		Touching = false
		First = 0
	end

Whats the size of the part? CHARACTER LIMIT

its like 70 by 70 by 70.s…,…,.,.,.,.,.,

The script has basically the same problems as the last script but the points multiplying only happens about 20% of the time

I dont know how to solve it so good luck!

1 Like