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

Workspace.Tool.Handle.Script:7: attempt to index nil with 'Hit'?

Asked by
Cikeruw 14
2 years ago

I am trying to make a gun script but it gives this error Workspace.Tool.Handle.Script:7: attempt to index nil with 'Hit'. How do I fix this?

here is my code

local Tool = script.Parent.Parent local handle = script.Parent

Tool.Equipped:Connect(function()
    game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player)
        local Mouse = Player:GetMouse()
        local direction = (Mouse.Hit.p - handle.Position).Unit
        local Ray = Ray.new(handle.Position,direction)
        local Part = Instance.new("Part")
        Part.Parent = workspace
        Part.Anchored = true
        Part.Size = Ray.Direction - Ray.Origin
        Part.Size = Part.Size + Vector3.new(0.5,0.5,0.5)
    end)
end)

1 answer

Log in to vote
0
Answered by 2 years ago

You can also get the mouse from Tool.Equipped event. Try this:

local Tool = script.Parent.Parent
local handle = script.Parent

Tool.Equipped:Connect(function(Mouse)
    game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
        local direction = (Mouse.Hit.Position - handle.Position).Unit
        local Ray = Ray.new(handle.Position, direction)
        local Part = Instance.new("Part")
        Part.Parent = workspace
        Part.Anchored = true
        Part.Size = Ray.Direction - Ray.Origin
        Part.Size = Part.Size + Vector3.new(0.5,0.5,0.5)
    end)
end)
Ad

Answer this question