How to check if equipped?

So basically im making a ability thats in a tool, and I just finished the ability, however. . .
the problem is that my equipped checker is haulting the entire script

local tool = script.Parent
local UIP = game:GetService("UserInputService")
local Remote = tool:WaitForChild("RemoteEvent")
local player = game.Players.LocalPlayer

local char = player.Character
local root = char:WaitForChild("HumanoidRootPart")
local Debounce = 1
local anim = script.Animation

local track = char.Humanoid:LoadAnimation(anim)
local ic = script.InstaCast

UIP.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E and Debounce == 1 then
		print("Local Script Recieved Input . . .")
		Debounce = 2
		spawn(function()

		ic = true
		wait(0.41)
		ic = false
			
		end)
		root.Anchored = true
		track:Play()
		wait(0.15)
		track:AdjustSpeed(0)
	end
end)

UIP.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E and Debounce == 2 then
		repeat wait() until ic == false
		print(". . . Sending To Server")
		track:AdjustSpeed(1)
		Debounce = 3
		Remote:FireServer(char, player, UIP, tool)
		wait(4) -- cooldown
		Debounce = 1
	end
end)

this is the script, I tried implementing integers like how debounce is used, and also tried external bool values, I tried server scripts and locals, however it never worked…

equippednotworking.rbxl (216.1 KB)

Or you can just check if the parent of the tool is the player’s character.

but the problem is that when the tool isn’t equipped/being held out it still can be activated. . .
this only solves the problem if im not holding it

So you don’t want to run it at all?

use this code to check if tool equipped, and before activation check this value:

local equipped = false
script.Parent.Equipped:Connect(function()
	equipped = true
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
end)

if you talking about being tool in inventory of player, you can just check if it keeps in player backpack or player model. if nor then tool not being equipped, makes sense

This is a very basic and easy to edit system for monitoring whenever a Tool instance is equipped by the player (or if a instance is added the player with the object class of Tool I guess)

This script should be placed in a Script instance within ServerScriptService . The function onToolEquipped is connected to each player that joins the game.

-- Function to detect when a player is holding a tool
local function onToolEquipped(player)
    player.CharacterAdded:Connect(function(char)
        character.ChildAdded:Connect(function(child)
            if child:IsA("Tool") then
                print(player.Name .. " has equipped a tool: " .. child.Name)
                -- Add additional logic here for when the player equips a tool
            end
        end)

        character.ChildRemoved:Connect(function(child)
            if child:IsA("Tool") then
                print(player.Name .. " has unequipped a tool: " .. child.Name)
                -- Add additional logic here for when the player unequips a tool
            end
        end)
    end)
end

-- Connect the function to each player that joins the game
game.Players.PlayerAdded:Connect(onToolEquipped)

It waits for the player’s character to be added and then listens for any tools (objects of class Tool ) being added to or removed from the character’s model. When a tool is equipped or unequipped, it prints a message to the output. You can replace the print statements with your own custom logic depending on what you want to happen when a tool is equipped or unequipped.

1 Like

this didn’t work and hasn’t worked, like 12 people have given the same idea

your saying “a tool” not sure how this helps

Have you tried using the tool.Parent? If the tool is equipped, the tool’s parent will be a model (aka the character).

`
local tool = script.Parent

local function isEquipped()
if tool.Parent:IsA(“Model”) then
return true
else
return false
end
`

good idea, however… not familiar with this code, wouldn’t know how to implement

I recommend you do something like this (LocalScript inside of your Tool):

local contextActionService = game:GetService("ContextActionService")

local tool = script.Parent

tool.Equipped:Connect(function()
	contextActionService:BindAction(tool.Name, function(_, inputState)
		if inputState == Enum.UserInputState.Begin then
			-- Write the code you want to happen when the player presses 'E' inside here, as an example:
			print(true)
		else
			-- Write the code you want to happen when the player stops pressing 'E' inside here, example:
			print(false)
		end
	end, false, Enum.KeyCode.E)
end)

tool.Unequipped:Connect(function()
	contextActionService:UnbindAction(tool.Name)
end)

It will only run the code if the player presses or depresses E while your Tool is equipped

Try this out, this is the script that i created, i used localscript

local Tool = script.Parent

local function Equipped()
print(game.Players.LocalPlayer.UserId…" Equipped the tool")
end

local function UnEquipped()
print(game.Players.LocalPlayer.UserId…" UnEquipped the tool")
end

Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(UnEquipped)

I Tried using Local functions and it seems to be working for me

out of all of them this worked

1 Like

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