I’m making a gun script but it can’t get the hit function from the GetMouse() function, I’ve started the game and opened the script and tested if it allowed the GetMouse() function and it did yet it returned this:
17:23:18.011 Mouse object is nil - Server - Server:32
17:23:18.011 Players.killersuperlegend.Backpack.Gun.Handle.Server:35: attempt to index nil with 'Hit' - Server - Server:35
17:23:18.011 Stack Begin - Studio
17:23:18.011 Script 'Players.killersuperlegend.Backpack.Gun.Handle.Server', Line 35 - Studio - Server:35
17:23:18.011 Stack End - Studio
When I click with the mouse.
Here’s the code:
local Tool = script.Parent.Parent
local Sounds = script.Parent.Sounds
Tool.Equipped:Connect(function()
Sounds.Equip:Play()
end)
Tool.Unequipped:Connect(function()
Sounds.Unequip:Play()
end)
Tool.Activated:Connect(function()
Sounds.Activated:Play()
script.Parent.Shot.GunShotLight.Enabled = true
script.Parent.Shot.Transparency = 0
script.Parent.Shot.Smoke1.Enabled = true
script.Parent.Shot.Smoke2.Enabled = true
script.Parent.Dispenser.s1.Enabled = true
script.Parent.Dispenser.s2.Enabled = true
script.Parent.Dispenser.s3.Enabled = true
script.Parent.Dispenser.s4.Enabled = true
script.Parent.Dispenser.s5.Enabled = true
script.Parent.Dispenser.Decal1.Transparency = 0
script.Parent.Dispenser.Decal2.Transparency = 0
local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)
local mouse = player:GetMouse()
if mouse then
print("Mouse object is valid")
print(mouse.Hit)
else
print("Mouse object is nil")
end
local ray = Ray.new(script.Parent.Position, (mouse.Hit - script.Parent.Position).unit * 100)
local part, hitPos = workspace:FindPartOnRay(ray, player.Character)
if part and part.Parent and part.Parent:FindFirstChild("Humanoid") and part.Parent ~= player.Character then
local targetHumanoid = part.Parent:FindFirstChild("Humanoid")
if targetHumanoid.Parent ~= script.parent.parent.parent then -- blacklist the player holding the gun
targetHumanoid.Health = targetHumanoid.Health - 30
end
end
wait(0.01)
script.Parent.Shot.Transparency = 1
script.Parent.Shot.GunShotLight.Enabled = false
script.Parent.Shot.Smoke1.Enabled = false
script.Parent.Shot.Smoke2.Enabled = false
script.Parent.Dispenser.s1.Enabled = false
script.Parent.Dispenser.s2.Enabled = false
script.Parent.Dispenser.s3.Enabled = false
script.Parent.Dispenser.s4.Enabled = false
script.Parent.Dispenser.s5.Enabled = false
script.Parent.Dispenser.Decal1.Transparency = 1
script.Parent.Dispenser.Decal2.Transparency = 1
end)
I’ve tried using ChatGPT to debug the code but it doesn’t work and I don’t know what to search for to find the solution.
What is the problem with the code?