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

Where should I put a debounce to prevent these remote events from firing multiple times?

Hi Scripting Helpers, this is a longer one.

I am trying to make a GUI inventory system very similar to the one in Soybeen's Booga Booga. I have gotten fairly close except that when you open the inventory multiple times and then try to drop or eat something, the amount of items eaten or dropped will equal the number of times you opened the inventory.

So if I open the inventory only once, it works exactly how I want it to, but if I close the inventory and re-open it, when you try and drop something it will drop 2 items instead of one. If I close and reopen it again it will drop 3 items and so on. This is due to "C" key being pressed multiple times.

So basically I just don't know where the logical place to put the debounce is.

I am using the following code.

--LOCAL SCRIPT

userInputService = game:GetService("UserInputService")
player = game.Players.LocalPlayer
playerGUI = player:WaitForChild("PlayerGui")
-- The following 2 items are remote events
dropRequest = game.ReplicatedStorage.DropRequest
imageButtonActivated = game.ReplicatedStorage.InventoryImageButtonActivate
--GUI
inventoryHolderGui = playerGUI.InventoryScreenGui.InventoryHolderFrame
inventoryMainFrame = inventoryHolderGui.InventoryFrame
isInventoryOpen = false
inventoryContents = player.Inventory:GetChildren()

userInputService.InputBegan:Connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.C and isInventoryOpen == false then
		inventoryHolderGui.Visible = true
		isInventoryOpen = true
		local inventoryButtons = inventoryMainFrame:GetChildren()
		for i,v in pairs(inventoryContents) do
			for o,b in pairs(inventoryButtons) do
				if b.Name == v.Name then
					if v.Value > 0 then	
						b.Visible = true
						b.AmountDisplay.Text = v.Name .. " : " .. tostring(v.Value)
						b.MouseButton2Down:Connect(function()
							--Problem may be here?
							dropRequest:FireServer(v)
						end)
						b.MouseButton1Down:Connect(function()
							--Problem may be here?
							imageButtonActivated:FireServer(v)
						end)
						v.Changed:Connect(function(value)
							if value > 0 then
								b.Visible = true
								b.AmountDisplay.Text = v.Name .. " : " .. tostring(v.Value)
							elseif value <= 0  then
								value = 0
								b.Visible = false
							end
						end)
					elseif v.Value <= 0 then
						b.MouseButton2Down:Connect(function()
							--Problem may be here?
							dropRequest:FireServer(v)
						end)
						b.MouseButton1Down:Connect(function()
							--Problem may be here?
							imageButtonActivated:FireServer(v)
						end)
						v.Changed:Connect(function(value)
							if value > 0 then
								b.Visible = true
								b.AmountDisplay.Text = v.Name .. " : " .. tostring(v.Value)
							elseif value <= 0  then
								value = 0
								b.Visible = false
							end	
						end)
					end
				end
			end
		end
	elseif input.KeyCode == Enum.KeyCode.C and isInventoryOpen == true then
		inventoryHolderGui.Visible = false
		isInventoryOpen = false
	end
end)

--SERVER SCRIPT


dropRequest = game.ReplicatedStorage.DropRequest
imageButtonActivated = game.ReplicatedStorage.InventoryImageButtonActivate
player = game.Players.PlayerAdded:Connect(function(plr)
	return plr
end)

dropRequest.OnServerEvent:Connect(function(player,pickupIntValue,inventoryOpenedTimes)
	local inventoryContents = player.Inventory:GetChildren()
	local PickupsContents = game.ServerStorage.Pickups:GetChildren()
	for i,v in pairs(inventoryContents) do
		if v.Name == pickupIntValue.Name then
			v.Value = v.Value - 1
		end
	end
	for i,v in pairs(PickupsContents) do
		if v.Name == pickupIntValue.Name then
			local droppedItem = v:Clone()
			droppedItem.Parent = game.Workspace
			droppedItem.CFrame = 							player.Character.Head.CFrame:ToWorldSpace(CFrame.new(-4,0,0))
		end
	end
end)

imageButtonActivated.OnServerEvent:Connect(function(player,value)
	if value.Name == "Apple" then
		value.Value = value.Value - 1
	elseif value.Name == "Bread" then
		value.Value = value.Value - 1
	end
end)

Thanks for the help!

1 answer

I think the solution would be to use 'debounce', The InputBegan event fires multiple times and not once (which is kind of weird)

1 comment

0

what line do u think I should put it on?

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