Help with Tool.Equipped or Tool.Activated

Hello.

This post is a followup to this post: (Help with :ClearAllChildren() and RemoteEvents) which is part one to the issues of a placement system. As resolved in that post, the cones now get deleted however WHENEVER the “R” key is pressed, not just when the tool is equipped. Even when chatting it still does this. I have tried multiple methods such as a debounce and Tool.Activated but neither methods work.

Help would be appreciated! Snippets of the code can be found below. (Only parts relating to the deletion of cones!)

LocalScript: “ConeSystem” (inside Tool: “Cone Placer”)

local UIS = game:GetService("UserInputService")
local Tool = script.Parent

Tool.Equipped:Connect(function()
	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.R then
			game.ReplicatedStorage.ClearCone:FireServer()
		end
	end)
end)

ServerScript: “ConeSystem” (inside Workspace)

game.ReplicatedStorage.ClearCone.OnServerEvent:Connect(function()
	game.Workspace.PlacedCones:ClearAllChildren()
end)

FYI, I’m not a massive scripter and I’m new to scripting.

local UIS = game:GetService("UserInputService")
local Tool = script.Parent

Tool.Equipped:Connect(function()
	UIS.InputBegan:Connect(function(input, gameProcessedEvent)
if not gameProcessedEvent then return end
		if input.KeyCode == Enum.KeyCode.R then
			game.ReplicatedStorage.ClearCone:FireServer()
		end
	end)
end

I don’t think I made myself clear enough in the original post, my apologies.

I want it so that when they press R holding the tool the cones get removed, not when they type that in chat.

Due to this being a CONTEXT ACTION, use ContextActionService. This should make it easy for you.

  1. Make a function for what you want to happen when r is pressed.
  2. When the tool is equipped, use ContextActionService:BindAction(actionName, function, createmobilebuttons, keycode (Enum.KeyCode.R))
  3. When the tool is Unequipped, use ContextActionService:UnbindAction(actionName)

if you’d like to use userinputservice (dont lmao) you need to make a variable that’s set to true when your tool is equipped, and set to false when it’s unequipped, then simply check if that variable is true when the button is pressed (not inside the equipped event)

1 Like

Thanks, I’ll have a read now and get back to you if need be. :slight_smile:

1 Like

[redacted]

found a solution around a bug using a debounce method, thanks so much for the help!

1 Like

Really quickly, just curious what the bug was. If the gun was firing twice, that’s because contextactionservice fires when the action starts, AND ends, you can fix this with the arguments that it returns, actionname (you wont need this) and userinputstate

function(actionname, state)

now just check if the state is equal to the beginning
if not state == Enum.UserInputState.Begin then return end

The bug was that it would place the cone straight after I had re-equipped the tool but I resolved it, thanks for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.