Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Touch Event Working But TouchEnded Event Not Working?

I'm making a shop, my Touch event's working perfectly but for some reason the TouchEnded one isn't working at all? I tried the code sample on Roblox TouchEnded page and it worked perfectly so it's not the script type or parent problem

LocalScript in StarterPlayerScripts:

local db = false

game.Workspace.Shop.Shop.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if db == false then
			db = true
			print("shop activated")
		end
	end
end)

game.Workspace.Shop.Shop.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if db == false then
			db = true
			print("shop deactivated")
		end
	end
end)

db = false

2 answers

✓ Accepted answer

##Problem:

The reason .TouchedEnded didn't work properly, was because of your debounce function.

Everything else is fine, except for your debounce.

##Information:

The error was caused because you checked to see if debounce was false in lines 5 -6, but you didn't actually put a wait in between the debounce, so essentially it would spam "shop activated" and debounce would have no use. It's also better to end the debounce in the function rather than outside the scope.

On line 16, you're checking to see if debounce is false, since you set debounce to true on line 7, that wouldn't run the code, because debounce isn't false.

##Fixed Script:

local db = false

game.Workspace.Shop.Shop.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and db  == false then
		db = true
		wait(1)
		print("shop activated")
		db = false
	end
end)

game.Workspace.Shop.Shop.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and db == false then
		db = true
		wait(1)
		db = false
		print("shop deactivated")
	end
end)

OR:(You could do this, although, the above script is more efficient:)

local db = false

game.Workspace.Shop.Shop.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if db == false then
			db = true
			wait(1)
			print("shop activated")
			db = false
		end
	end
end)

game.Workspace.Shop.Shop.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if db == false then
			db = true
			wait(1)
			print("shop deactivated")
			db = false
		end
	end
end)

#####Any more edits are gonna be on the wording in the answer!

Hey, it looks like you're using db as a variable that determines whether the shop is open. If that is the case, you should switch the order of db's value comparison and assignment in the TouchEnded function, like so:

if db == true then
	db = false
	print("shop deactivated")

3 comments

0

I didn't quite understand what you meant by switching value comparison and assignment, do I just switch the true and false values in the code?

0

oh just in the touchended function

0

Yeah, sorry I should have made it clearer bro

This discussion is preserved as it was. New answers, comments, and votes are closed.