How to give points when standing on a part?

I want to make a platform that adds 1 coin every second in the Coins leaderstat. I want to make this work when they are standing on it and make it work even when they are standing still. I don’t really know how to go about this and if you have any ideas on how to do this, reply to this topic.

1 Like

Have you tried triggers? Basically it’s an invisible block and when collided with it it triggers an event like gives coins or triggers cut scenes. This technique is used with the game ‘piggy’.

1 Like

I don’t want it to be a one time function. I want it to give the player a coin every second when they are standing on the part. Do you know how to code it by chance?

1 Like

Using loops you can add a +1 value into the leaderstat

Let me know if you need an example!

1 Like

How would I activate it and still tell if the player is still standing on the part?

And yes I need an example.

something along the lines of this, but setting it to your leaderstat clearly lol

local part = script.Parent

local function onPartTouched(otherPart)
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		leaderstat +1 -- clearly not leaderstat, but referencing to your leaderstat you want to add to
	end
end

part.Touched:Connect(onPartTouched)

i basically took this right out of the dev forum
https://developer.roblox.com/en-us/articles/detecting-collisions

1 Like

Do I just put this script under the part that they are going to be touching.

And how will I find the player to give the leaderstats to? Because I can’t edit leaderstats with the humanoid.

  1. Put a script under the part.
  2. Put this inside the script:
local part = script.Parent
local getparts = part:GetTouchingParts()

for i, v in pairs(getparts) do
   if v:IsA("BasePart") then
     if v.Parent:FindFirstChildOfClass("Humanoid") then
         local plr = game.Players:GetPlayerFromCharacter(v.Parent)
         if plr ~= nil then
            while wait() do
               plr.leaderstats.Coins.Value += 1
            end
         end
     end
  end
end

try it out

This script is not working and I put print functions to see if it runs but it does not.

local part = script.Parent
local getparts = part:GetTouchingParts()

for i, v in pairs(getparts) do
	print("Hello")
	if v:IsA("BasePart") then
		if v.Parent:FindFirstChildOfClass("Humanoid") then
			local plr = game.Players:GetPlayerFromCharacter(v.Parent)
			if plr ~= nil then
				while wait(1) do
					print("Yes")
					plr.leaderstats.Current.Value += 1
				end
			end
		end
	end
end

u have to change the “plr.leaderstats.Coins.Value” to the path of ur stat

This doesn’t work because it only checks the touching parts immediately after the game runs. Try this instead:

local part = script.Parent
local touching = false

part.Touched:Connect(function(touchedPart)
	if touchedPart:IsA("BasePart") then
		if touchedPart.Parent:FindFirstChildOfClass("Humanoid") then
			local plr = game.Players:GetPlayerFromCharacter(touchedPart.Parent)
			if plr then touching = true
				repeat task.wait(1)
					plr.leaderstats.Current.Value += 1
				until not touching
			end
		end
	end
end)

part.TouchEnded:Connect(function(touchedPart)
counting = false
end)
1 Like
local part = script.Parent
local players = game:GetService("Players")
local debounce = false
part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		debounce = true
		local character = hit.Parent
		local player = players:GetPlayerFromCharacter(hit.Parent)
		if player then
			local leaderstats = player:FindFirstChild("leaderstats")
			if leaderstats then
				local current = leaderstats:FindFirstChild("leaderstats")
				if current then
					current.Value += 1
				end
			end
		end
		task.wait(2) --2 second cooldown between touches
		debounce = false
	end
end)
local part = script.Parent
local players = game:GetService("Players")
local debounces = {}

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local character = hit.Parent
		local player = players:GetPlayerFromCharacter(hit.Parent)
		if player then
			if table.find(debounces, player) then
				return
			end
			table.insert(debounces, player)
			local leaderstats = player:FindFirstChild("leaderstats")
			if leaderstats then
				local current = leaderstats:FindFirstChild("leaderstats")
				if current then
					current.Value += 1
				end
			end
			task.wait(2)
			if table.find(debounces, player) then
				table.remove(debounces, table.find(debounces, player))
			end
		end
	end
end)

This script does work but only if I am running on the part. When I stand still it does nothing. Can you maybe make it work so that when you are standing on it, it will still work?

Raycast down every second and if it hits the block you want it to hit then add a point, or you can raycast at a faster rate but with a 1 second debounce

1 Like