How do I keep touch event going

  1. What do you want to achieve? Keep it simple and clear!
    Trying to make touch event keep going instead of stopping when player doesn’t move

  2. What is the issue? Include screenshots / videos if possible!
    The touch event keep stopping when player doesn’t move inside the detecting part

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Searching but still got none solutions

as you can see in the video the number on the right change when I move but stop when I don’t move. I want to keep it going when don’t move

local players = game:GetService("Players")
local deb = false

script.Parent.Touched:Connect(function(hit)
    if deb == false then
        local ply = players:GetPlayerFromCharacter(hit.Parent)

        local Bricks = ply:FindFirstChild("Bricks")
        local Gifts = ply:FindFirstChild("Gifts")

        if Bricks.Value >= 5 then
            deb = true
            Bricks.Value = Bricks.Value - 5
            Gifts.Value = Gifts.Value + 1
            wait(0.2)
            deb = false
        end
    end
end)
1 Like

This is by design an event will trigger when something happens then leave it up to you to do whatever is needed, the event is just the trigger. In your case you probably want to use the event as a trigger to start a coroutine that runs until the player moves off of the SpawnLocation. This would mean the coroutine would need to check if the player is still within the bounds of the SpawnLocation, a simple way to do that would be to have some invisible parts sourrounding SpawnLocation and use the Touched event on these to stop the coroutine.

I don’ t have time to give you a sample now, but will check back later and do so if noone else has.

You can use .Touched to begin the loop, continuously do whatever then use .TouchEnded to end the loop (when player leaves the part)

1 Like

Good spot, I assumed that was just to indicate the Touched event had completed. Should RTFM a bit more :slight_smile:

local players = game:GetService("Players")
local part = script.Parent
local deb = false
local isTouching = false

part.Touched:Connect(function(hit)
	if deb == false then
		local ply = players:GetPlayerFromCharacter(hit.Parent)
		if ply then
			isTouching = true
			local Bricks = ply:FindFirstChild("Bricks")
			local Gifts = ply:FindFirstChild("Gifts")
			if Bricks and Gifts then
				task.spawn(function()
					while isTouching do
						task.wait()
						if Bricks.Value >= 5 then
							deb = true
							Bricks.Value -= 5
							Gifts.Value += 1
						end
					end
					return
				end)
			end
		end
	end
	task.wait(0.2)
	deb = false
end)

part.TouchEnded:Connect(function(hit)
	local ply = players:GetPlayerFromCharacter(hit.Parent)
	if ply then
		isTouching = false
	end
end)

Responded to the wrong user but this is essentially what you should be looking to achieve.

2 Likes