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

How do I equip and activate a tool from a script?

Hello, I was trying to do a weapon that can equip and activate another tool when right-clicking. The script works for me on studio but when I try it in the real game, it equips the tool but doesnt do anything. Here's my script:



local v = game.Players.LocalPlayer
local vm = v:GetMouse()
local w = script.Parent.Parent:WaitForChild("Bomb") 
local t = script.Parent
local used = false
t.Equipped:Connect(function(Mouse)
	Mouse.Button2Down:connect(function()
		if not used then
			used = true
			t.AutoEquip.Disabled = true
			v.Character.Humanoid:EquipTool(w)
			wait(0.1)
			w:Activate()
			game.Workspace:WaitForChild("TimeBomb"..v.Name)
			t.AutoEquip.Disabled = false
			wait(5)
			used = false
		end
	end)
end)

Everything is in the explorer place it should be. In the real game console, it returns: Attempt to index a nil value

Thanks

5 comments

0

Please put the script in a codeblock.

0

Also what line is it printing nil value for also?

0

sorry... i am new... how do I put the script in a codeblock?

0

Put four spaces or a tab before each line. I had trouble with that at first too.

0

Confirm that "TimeBomb" with the players name is in workspace at the time it prints Nil

1 answer

✓ Accepted answer

###This is happening because you used a server script. LocalPlayer is nil on the server, so to fix this simply make your Script a LocalScript. I also tidied up your code a bit. Please remember to put your code in code blocks, by clicking the blue Lua icon and pasting the code in between the tildes.

-- Local Script!! And please have good variable names 

local player = game.Players.LocalPlayer 
local mouse = player:GetMouse() 
local bomb = script.Parent.Parent:WaitForChild("Bomb") 
local tool = script.Parent 
local used = false 

tool.Equipped:Connect(function(Mouse) 
    Mouse.Button2Down:Connect(function() -- :connect is deprecated, use :Connect
       if not used then 
           used = true 
               tool.AutoEquip.Disabled = true 
               player.Character.Humanoid:EquipTool(bomb) 

               wait(0.1) 

               bomb:Activate() 
               game.Workspace:WaitForChild("TimeBomb"..player.Name) 
               tool.AutoEquip.Disabled = false 
               wait(5) 
               used = false
         end 
    end) 
end)


7 comments

0

I tried it, but it keeps not working.

0

Also it was already in a LocalScript

0

yeah he just forgot to change player:GetMouse()

0

yeah.

0

what do you mean?

0

I have been testing some things and the error is in the line 11 of my script. v.Character.Humanoid is valid. The nil value must be in :EquipTool(w)?

0

I found the problem, another script was breaking this when it changed tools

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