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
The reason .TouchedEnded
didn't work properly, was because of your debounce function.
Everything else is fine, except for your debounce.
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.
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)
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")