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

ContextActionService firing multiple times, help? [SOLVED]

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

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)

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