Constantly Detect Touch On Part

  1. What do you want to achieve?
    I’m trying to make a part that adds money when you stand on it.

  2. What is the issue?
    It detects a touch but then stops right after. It doesn’t keep detecting that the player is touching the part and you have to move around for it to detect.

  3. What solutions have you tried so far?
    I tried adding a debounce to see if that would work but it didn’t.

local Players = game:GetService("Players")

local Requirement = 100
local Multiplier = 5

local particleEffect = script.Parent:WaitForChild("Particles")

function onTouch(Touch)
	local Player = Players:GetPlayerFromCharacter(Touch.Parent)
	if Player ~= nil then
		if Player.leaderstats["Cash"].Value >= Requirement then
			particleEffect.Enabled = true
			Player.leaderstats["Cash"].Value = Player.leaderstats["Cash"].Value - Requirement
			Player.leaderstats["Multiplier"].Value = Player.leaderstats["Multiplier"].Value + Multiplier
		end
	end
end

script.Parent.Touched:Connect(onTouch)

function notTouch(Touch)
	particleEffect.Enabled = false
end

script.Parent.TouchEnded:Connect(notTouch)
2 Likes

After detecting a touch event, you could use part:GetTouchingParts() and keep checking that every so often until the player is no longer touching the part.

3 Likes

This post lists a few different methods of checking if a player is in a region: Zone+ v1 (deprecated) | Retrieving players within an area/zone

( You could also use Zone+ lol )

2 Likes

This comes as a result of you only detecting the .Touched, not looping it through until a variable defines it as no longer being touched. Some simple code touch ups and we produce this:

local Players = game:GetService("Players")

local Requirement = 100
local Multiplier = 5

local particleEffect = script.Parent:WaitForChild("Particles")

function onTouch(Touch)
	local Player = Players:GetPlayerFromCharacter(Touch.Parent)
	if Player ~= nil then
repeat wait()
		if Player.leaderstats["Cash"].Value >= Requirement then
			particleEffect.Enabled = true
                        
			Player.leaderstats["Cash"].Value = Player.leaderstats["Cash"].Value - Requirement
			Player.leaderstats["Multiplier"].Value = Player.leaderstats["Multiplier"].Value + Multiplier
                        	end
until not particleEffect.Enabled
	
	end
end

script.Parent.Touched:Connect(onTouch)

function notTouch(Touch)
	particleEffect.Enabled = false
end

script.Parent.TouchEnded:Connect(notTouch)
2 Likes

It kind of works and kind of doesn’t. It constantly detects but when I step off it still keeps going.

1 Like

I’ve done this before recently, and a method I’ve considered trying is using a while true loop and a touch ended function. For instance:

local Players = game:GetService("Players")

local Requirement = 100
local Multiplier = 5

local particleEffect = script.Parent:WaitForChild("Particles")

function onTouch(Touch)
    local Player = Players:GetPlayerFromCharacter(Touch.Parent)
    if Player ~= nil and not Player:FindFirstChild("AddingMoney") then
        local AddingMoney = Instance.new("IntValue")
        AddingMoney.Name = "AddingMoney"
        AddingMoney.Parent = Player
    end
end

script.Parent.Touched:Connect(onTouch)

function notTouch(Touch)
    particleEffect.Enabled = false
    local Player = Players:GetPlayerFromCharacter(Touch.Parent)
        if Player ~= nil then
            for _, value in pairs(Player:GetChildren()) do --Checks for All Values 
                if value.Name == "AddingMoney" then
                    value:Destroy()
                end
            end    
        end
    end
end)

while wait(1) do --ADDING MONEY WAIT TIME
    for _, Player in pairs(Players:GetPlayers()) do
        if not Player:FindFirstChild("AddingMoney") then return end
        if Player.leaderstats["Cash"].Value >= Requirement then
            particleEffect.Enabled = true
            Player.leaderstats["Cash"].Value = Player.leaderstats["Cash"].Value - Requirement
            Player.leaderstats["Multiplier"].Value = Player.leaderstats["Multiplier"].Value + Multiplier
        end
    end
end

Basically, I added an “AddingMoney” Value to the player while it’s still in contact with the part, and deleted this value once it’s not in contact. There’s a while loop checking through the players every second to see if there’s a value called “AddingMoney”, and if there is, the function is performed.

1 Like

That’s my mistake, apologies, I forgot to move the particleEffect.Enabled = true outside of the repeat argument, thus the repeat loop would never find a value that causes it to stop. This should fix the issue.

local Players = game:GetService("Players")

local Requirement = 100
local Multiplier = 5

local particleEffect = script.Parent:WaitForChild("Particles")

function onTouch(Touch)
	local Player = Players:GetPlayerFromCharacter(Touch.Parent)
	if Player ~= nil then
			particleEffect.Enabled = true
repeat wait()
		if Player.leaderstats["Cash"].Value >= Requirement then
                        
			Player.leaderstats["Cash"].Value = Player.leaderstats["Cash"].Value - Requirement
			Player.leaderstats["Multiplier"].Value = Player.leaderstats["Multiplier"].Value + Multiplier
                        	end
until not particleEffect.Enabled
	
	end
end

script.Parent.Touched:Connect(onTouch)

function notTouch(Touch)
	particleEffect.Enabled = false
end

script.Parent.TouchEnded:Connect(notTouch)
2 Likes

Thank you so much, that fixed it!