Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago
Edited 6 years ago

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

0
Please put the script in a codeblock. LegitmateTrades 159 — 6y
0
Also what line is it printing nil value for also? LegitmateTrades 159 — 6y
0
sorry... i am new... how do I put the script in a codeblock? VewixxPlayer 67 — 6y
0
Put four spaces or a tab before each line. I had trouble with that at first too. OBenjOne 190 — 6y
0
Confirm that "TimeBomb" with the players name is in workspace at the time it prints Nil LegitmateTrades 159 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)


0
I tried it, but it keeps not working. VewixxPlayer 67 — 6y
0
Also it was already in a LocalScript VewixxPlayer 67 — 6y
0
yeah he just forgot to change player:GetMouse() MaxDev_BE 55 — 6y
0
yeah. User#19524 175 — 6y
View all comments (3 more)
0
what do you mean? VewixxPlayer 67 — 6y
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)? VewixxPlayer 67 — 6y
0
I found the problem, another script was breaking this when it changed tools VewixxPlayer 67 — 6y
Ad

Answer this question