Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Touch Event Working But TouchEnded Event Not Working?

Asked by 2 years ago

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

Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

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!
Ad
Log in to vote
0
Answered by 2 years ago

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")
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? SuperSM1 67 — 2y
0
oh just in the touchended function SuperSM1 67 — 2y
0
Yeah, sorry I should have made it clearer bro nikoviking 236 — 2y

Answer this question