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

Why is an event fired 25 times when touching something once?

When I test the game and go in the area pressing e the character's entire inventory is filled. Here is a local script under StarterCharacterScripts:

local uis = game:GetService("UserInputService")
local inArea = false
local player = game.Players.LocalPlayer 
workspace.LootBox.AREA.Touched:Connect(function()
 inArea = true
    print("entered")
    workspace.LootBox.Part.BillboardGui.Enabled = true

 	 uis.InputBegan:Connect(function(input) 
       if input.UserInputType.Name == "Keyboard" and input.KeyCode.Name == "E" and inArea == true then
    	print("THIS WORKS IF PRINTED")
wait(1)
game.ReplicatedStorage.CloneTool:FireServer(player)
	end
    end)
    end)

        workspace.LootBox.AREA.TouchEnded:Connect(function()
            workspace.LootBox.Part.BillboardGui.Enabled = false
            inArea = false
            print("left")
        end)

Here is a script under ServerScriptService:

game.ReplicatedStorage.CloneTool.OnServerEvent:Connect(function(player)
	
	local Tool = game.ServerStorage.ClassicSword
local Part = script.Parent
    local Clone = Tool:Clone()
    Clone.Parent = player.Character
	
end)

Why is the event firing several times?

1 answer

✓ Accepted answer

you could try adding a debounce, so it might look something like this

local uis = game:GetService("UserInputService")
local inArea = false
local keyPressed = false--debounce
local player = game.Players.LocalPlayer 
workspace.LootBox.AREA.Touched:Connect(function()
 inArea = true
    print("entered")
    workspace.LootBox.Part.BillboardGui.Enabled = true

     uis.InputBegan:Connect(function(input) 
       if input.UserInputType.Name == "Keyboard" and input.KeyCode.Name == "E" and inArea == true and keyPressed == false then
		keyPressed = true
        print("THIS WORKS IF PRINTED")
wait(1)
game.ReplicatedStorage.CloneTool:FireServer(player)
wait(1)
keyPressed = false
    end
    end)
    end)

        workspace.LootBox.AREA.TouchEnded:Connect(function()
            workspace.LootBox.Part.BillboardGui.Enabled = false
            inArea = false
            print("left")
        end)

this would basically add a cooldown of 2 seconds before another item would be added to the players inventory

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