Touch Event only works once

I am trying to make it so that when a player touches a part a gui tweens.

It works the first time, but after the first time these errors pop up.


These are the script

    local db = false
    local StarterGui = game:GetService("StarterGui")
    script.Parent.Touched:connect(function(hit)
    		if hit.Parent:WaitForChild("Humanoid") then
    			local db = true
    			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    				local frame = player:WaitForChild("PlayerGui"):WaitForChild("Shop"):WaitForChild("ShopFrame")
    				frame:TweenPosition(UDim2.new(0.5,-250,0.5,-175),"Out","Quint",1,true)
    				print("opened")
    				wait(0.5)
    		end
    	db = false
    end)

A help would be very appreciated

that’s because you didn’t let the debounce have a rest. Change that script to this:

local db = false
local StarterGui = game:GetService("StarterGui")
script.Parent.Touched:connect(function(hit)
    if hit.Parent:WaitForChild("Humanoid") then
        if db == false then
    		db = true
    		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    		local frame = player:WaitForChild("PlayerGui"):WaitForChild("Shop"):WaitForChild("ShopFrame")
    		frame:TweenPosition(UDim2.new(0.5,-250,0.5,-175),"Out","Quint",1,true)
    		print("opened")
    		wait(0.5)
            db = false
    	end
    end
end)
2 Likes

The issue is that if the part touches anything that isn’t a Character then it still endlessly Waits for the humanoid.

You should instead use :FindFirstChild(“Humanoid”) and check if it is not equal to nil.

Edit; Also, as @TOP_Crundee123 said, you should always use a debounce for Touched events as they will fire for every part of the character that touches it. Basically, it will spam the function many times when you just wanted it to fire once.

3 Likes

If you want hats/hair/etc to also be able to trigger the touched event successfully, you need to add the second parent as a check for humanoid as well.

if hit.Parent:FindFirstChild(“Humanoid”) or hit.Parent.Parent:FindFirstChild(“Humanoid”) then

This is because the parts within the accoutrements are secondary children (they aren’t a direct child of the character). I guess they are grandchildren? :joy:

1 Like

Could you not just have debugged this yourself? If you fixed your indenting, you would realise very quickly what the problem is. Please, please, please be the developer who searches first and reviews their own code first before posting threads to this category asking for free fixes on a silver platter.

Don’t dump your code and errors in threads and ask for help when it’s unclear if you’ve actually attempted to resolve this issue yourself first.

2 Likes

@TOP_Crundee123 and @Hazania Thank u so much for your help.
@colbert2677 I am sorry, I was just really excited to get help. I will review the script carefully from now, instead of just asking for help when I don’t really need it.

I revised the code with the help of your helpful suggestion, however the gui still only tweens once and there`s no error this time
script
> local db = false

    local StarterGui = game:GetService("StarterGui")
    script.Parent.Touched:connect(function(hit)
    	wait(0.5)
    		if hit.Parent:FindFirstChild("Humanoid") then
    			db = true
    			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    				local frame = player:FindFirstChild("PlayerGui"):FindFirstChild("Shop"):FindFirstChild("ShopFrame")
    				frame:TweenPosition(UDim2.new(0.5,-250,0.5,-175),"Out","Quint",1,true)
    				print("opened")
    				wait(1)
    				db = false
    		elseif hit.Parent.Parent:FindFirstChild("Humanoid") then
    			db = true
    			local player = game.Players:GetPlayerFromCharacter(hit.Parent.Parent)
    				local frame = player:FindFirstChild("PlayerGui"):FindFirstChild("Shop"):FindFirstChild("ShopFrame")
    				frame:TweenPosition(UDim2.new(0.5,-250,0.5,-175),"Out","Quint",1,true)
    				print("opened")
    				wait(1)
    				db = false
    		end
    end)

@Hazania Do u have any solution that u can come up with?
Sorry for the ping

You don’t put a debounce to true that doesn’t have an if statement that detects if it’s false

local StarterGui = game:GetService("StarterGui")
local db = false
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
        if db = false then
			db = true
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			local frame = player:FindFirstChild("PlayerGui"):FindFirstChild("Shop"):FindFirstChild("ShopFrame")
			frame:TweenPosition(UDim2.new(0.5,-250,0.5,-175),"Out","Quint",1,true)
			print("opened")
			wait(1)
			db = false
        end
	end
end)

you don’t need an else if because you are just repeating te statement

1 Like
if db == false then

on line 5.

One = is for setting, two is for checking. And as others on the thread suggested, you need an or and include the second condition or you won’t pick up accoutrements.

if hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid") then

on line 4

1 Like

the db one was a typo, I can make mistakes sometimes.

1 Like

I mean there are many errors in your game but not even from this script. There are some functions in it which were deprecated but still works. Because of an error the script may timeout and breaks whole scripts down, get rid of all the errors and try again. I hope this helps :herb:

2 Likes