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

okay have a good night.,.,.,…,.,.,.,.,.,.,…,.,.,.,.,.,.,.,.,.,

Its really simple.

This is just a quick example

local part = script.Parent

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local points = leaderstats.Points
	player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")
		if not Humanoid then return end
		while part.Touched:Wait() do
			Points.Value += 1
			print(Points.Value)
		end
	end)
end)

Add a debounce if you need one

whats a debounce

.,.,.,.,…,.,.,.,.,…,.,.,.,.,…,.,.,.,.,.,.,.,.,.,.,…,

also it didnt work

Lemme recreate the whole thing. Give me 5min

ok …,.,.,…,.,…,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.<>.,.,…,.,.,><.,.,.,><.,.,.><.,><.,>.,

I think this will work for you

local Part = script.Parent
local touch = false
local plr = nil
local _delay = 5

while true do
	if touch == true and plr then
		plr.leaderboard.NumberValue = plr.leaderboard.NumberValue + 1
		task.wait(_delay)
	end
end

Part.Touched:Connect(function(hit)
	plr = hit.Parent
	touch = true
end)

Part.TouchEnded:Connect(function(hit)
	plr = nil
	touch = false
end)

it doesnt change the Value/…,…,.,.,

hit.parent is probably being set to some other part, you should check if its a player using that whole humanoid ~= nil thing

and the touch ended thing is an issue too, its gonna constnatly set plr to nil when something else stops touching. Instead you should make the true loop set “touch” to false everytime (and plr to nil)

local Part = script.Parent
local touch = false
local plr = nil
local _delay = 5

while true do
	if touch == true and plr then
		plr.leaderboard.NumberValue = plr.leaderboard.NumberValue + 1
		touch == false
		plr == nil
		task.wait(_delay)
	end
end

Part.Touched:Connect(function(hit)
	if not hit.Parent:FindFirstChild("Humanoid") then return end
	plr = hit.Parent
	touch = true
end)

might still not work tbh since i couldn’t test it

1 Like

I just wanted to try this out myself and see how far I could take it. I made a system that runs entirely on the server. It will check every second to see if there is a player on the platform. If there is, it will grant the player +1 ‘Capture’ point. It is functional in multiplayer use.

If it is functional in your scenario however, I would not know. What I do know, is that this will surely guarantee a +1 per second point increase. If it does not fit your scenario, I am sure this code will be a base to start off of to reach what your goal is.

local part = game.Workspace.Plate

local partsInPlate = {} --//Determines EVERYTHING touching the plate

local seenParts = {} --//These two tables will be used for filtering to make sure players do not get double points every second
local doubleParts = {}

while task.wait(1) do
	local Players = game:GetService("Players")
	
	local partsInPlate = game.Workspace:GetPartBoundsInBox(part.CFrame, part.Size + Vector3.new(0, 1, 0)) --//Determine the hitbox scan, which will be 1 stud up (Y+1)
	
	for i, v in pairs(partsInPlate) do --//Loop through the parts the hitbox was able to detect
		if v.Parent:FindFirstChild("Humanoid") then --//Determine which part belongs to a player
			partsInPlate[i] = v.Parent.Name --//If it is a player part, find and state the name of the model so we have the player name
		end
		
		if v.Name == "Baseplate" or v.Name == "Workspace" or v.Name == "Plate" then
			table.remove(partsInPlate, i) --//This loop removes unwanted parts from the hitbox detection
			continue
		end
		--[[ Credits to @MattVSNNL for the duplicate-filter. {https://devforum.roblox.com/t/how-to-remove-dupes-from-tables/951477/5}
		This next loop will take care of any duplicates. That is because when we looped through the partsInPlate
		table, we presumably picked up LeftLeg and RightLeg, resulting in two player names in the table. If we do not filter that out,
		the player will get two points. Why not just filter out one leg? For instance, if the player falls, every single part of their body will be
		in the table, not just their legs. So, to globally filter everything will give better results rather to filter by part.
		]]
		if not table.find(seenParts, v.Parent.Name) then --//If it can not find it in the 'seen'-table...
			table.insert(seenParts, v.Parent.Name) --//...then insert it in the table
		else
			table.insert(doubleParts, v.Parent.Name) --//If it is found in the 'seen'-table - meaning it is diplucate - insert it in the 'duplicates'-table.
		end
		
	end
	
	partsInPlate = seenParts --//Finally, update the original table with the new instances
	
	for i, v in partsInPlate do --//This loop will be used to get the actual player instance.
		if Players:FindFirstChild(v) then --//If there is a match from the table in the player list..
			game.Players[v].leaderstats.Capturing.Value += 1 --//Give that player +1 capture point
			
			print("Current players on the plate are; ", table.concat(partsInPlate, ", ")) --//Print the end result for debug purposes. Can be removed.
		end
	end
	
	table.clear(seenParts, doubleParts) --//If you do not clear the tables, they will think the player will be on the plate indefinitely. So to avoid that, clear the table.
	
end

I added notes for extra explanation. You can remove them if they serve no purpose.

3 Likes

Thanks so much for the solution :grinning: :grinning: :grinning: :grinning: :grinning:

1 Like

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