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

ContextActionService firing multiple times, help? [SOLVED]

Asked by 4 years ago
Edited 4 years ago

So I have a press e to enter shop thing and, well, when you press e, the shop event fires many, MANY times, making it impossible to use. How do I fix this? Code:

local function CloseShop()
    ReplicatedStorage.ShopEvents.Close:FireServer(player)
end

local function OpenShop()
    ReplicatedStorage.ShopEvents.Open:FireServer(player)
end

local db = false

while true do
        if player:DistanceFromCharacter(workspace.Shop.Counter.ShopPart.Position) <= 20 then
            if player:WaitForChild("InShop").Value == false then
                if not db then
                    db = true
                    ContextActionService:BindAction("OpenShop", OpenShop, true, "e")
                    wait(1)
                    db = false
                end
            elseif player:WaitForChild("InShop").Value == true then
                if not db then
                    db = true
                    ContextActionService:BindAction("CloseShop", CloseShop, true, "e")
                    wait(1)
                    db = false
                end
            else
                warn("Error with "..player.Name.."'s InShop value!")
            end
        else
            ContextActionService:UnbindAction("CloseShop", OpenShop, true, "e")
            ContextActionService:UnbindAction("OpenShop", CloseShop, true, "e")
        end
    wait(1)
end

I tried adding a debounce, to no avail.

1 answer

Log in to vote
0
Answered by 4 years ago

Never mind, didn't realize that the while true loop wasn't necessary! My fixed code:

local function HandleShop(actionName, inputState, inputObj)
    if inputState == Enum.UserInputState.Begin then
        if player:DistanceFromCharacter(workspace.Shop.Counter.ShopPart.Position) <= 20 then
            if player:WaitForChild("InShop").Value == false then
                ReplicatedStorage.ShopEvents.Open:FireServer(player)
            else
                ReplicatedStorage.ShopEvents.Close:FireServer(player)
            end
        end
    end
end

local db = false

ContextActionService:BindAction("HandleShop", HandleShop, true, Enum.KeyCode.E, Enum.KeyCode.ButtonY)
Ad

Answer this question