I am making damage dealt by guns rely on the position of the cursor on the screen, however when I shoot the npc or dummy bot I have put in to test, the console returns an error saying "attempt to index nil with 'Target' ". I'm slowly learning Lua but it can be challenging, any help would be highly appreciated!
There is more to the script but for simplicity I have included only the part with the issue. (Line 25 here)
-- global reload function -- local function Reload() Reloading = true wait(1) Ammo = MaxAmmo Reloading = false end -- holding item, connect -- script.Parent.Equipped:Connect(function() print("Pistol equipped") end) -- click to attack -- script.Parent.Activated:Connect(function(Mouse) print("fired "..Ammo) if Ammo > 0 and not Reloading then Ammo -= 1 print(Ammo) script.Parent.GunShot:Play() --recoils-- if Mouse.Target.Parent:FindFirstChild("Humanoid") then script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 20) end
Tool.Activated
does not have any parameter, which means the mouse argument is defaulted as nil, therefore there is no Target on nil values. You should make player:GetMouse()
to get the mouse and it will work.