Mouse.Hit.p not working correctly

I’m trying to make a 3rd person shooter game but my gun is being strange. It isn’t shooting the right way!
Here is a video.
robloxapp-20200427-1455482.wmv (1.9 MB)
I’m not sure what’s wrong as i used basically the same script as my other guns which work fine!
Here is my code as well.

---Local Script
wait()
local tool = script.Parent.Parent
local user
local firing = false
local origin = tool.Hole
local mouse = game.Players.LocalPlayer:GetMouse()
local mousehit = mouse.Hit.p

tool.Equipped:Connect(function()
script.Parent.Parent.Loaded:Play()
end)
mouse.Button1Up:Connect(function()
firing = false
end) 
mouse.Button1Down:connect(function()
firing = true
repeat
game.ReplicatedStorage.Events.Fire:FireServer(mousehit,origin)
wait(.1)
until firing == false or script.Parent.Parent.Ammo.Value == 0
end)
---Server Script
game.ReplicatedStorage.Events.Fire.OnServerEvent:Connect(function(player,mousehit,origin)
if script.Parent.Parent.Ammo.Value > 0 then
script.Parent.Parent.Ammo.Value = script.Parent.Parent.Ammo.Value - 1
local ray = Ray.new(origin.CFrame.p, (mousehit - origin.CFrame.p).unit * 300)
local hit, position = game.Workspace:FindPartOnRay(ray, player.Character)
origin.Parent.Fire:Play()
local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(10)
end
local distance = (position - origin.CFrame.p).magnitude
local rayPart = Instance.new("Part", player.Character)
rayPart.Name          = "RayPart"
rayPart.BrickColor    = BrickColor.new("New Yeller")
rayPart.Transparency  = 0.5
rayPart.Anchored      = true
rayPart.CanCollide    = false
rayPart.TopSurface    = Enum.SurfaceType.Smooth
rayPart.BottomSurface = Enum.SurfaceType.Smooth
rayPart.Material      = Enum.Material.Plastic
rayPart.formFactor    = Enum.FormFactor.Custom
rayPart.Size          = Vector3.new(0.2, 0.2, distance - 6)
rayPart.CFrame        = CFrame.new(position, origin.CFrame.p) * CFrame.new(0,0,-distance/2) 
game.Debris:AddItem(rayPart, 1/30)
script.Parent.Parent.Hole.Smoke.Enabled = true
wait(.1)
script.Parent.Parent.Hole.Smoke.Enabled = false
end
end)

it also comes back with some errors sometimes saying things like “Hit is a nil value”

2 Likes

This variable will not update when you move your mouse. So in the :FireServer call just replace mousehit with mouse.Hit.Position to get an updated position

2 Likes

It comes back with an error saying " [Players.ScriptToon.Backpack.PPSH.Scripts.Fire:4: attempt to index nil with ‘Hit’]"

You are likely passing a reference to the mouse to the server. The mouse object is created locally, so the server cannot see it. Only pass the position ( mouse.Hit.Position )

robloxapp-20200427-1508192.wmv (1.4 MB)
It just ends up doing this again :frowning:

It’s not Mouse.Hit.P(osition) that isn’t working properly, it’s your code. Mouse.Hit.P is just a reference to the Position of the CFrame set in Mouse.Hit. Sure you aren’t just setting things wrongly? You can use Making a Laser Gun as a reference. Though it lacks filtering, it can be used as a point of reference.

When the script loads you save the current mouse position. Then you keep sending that position to the server over and over again.

Should be:
game.ReplicatedStorage.Events.Fire:FireServer(mouse.Hit.p, origin)

1 Like

Thanks it worked! :smiley: Fires at my mouse now!

Why would mouse.Hit.Position return as an instance? Does anyone know, because I’m having that problem.

Mouse.Hit is a CFrame. CFrame.Position returns a Vector3 not an Instance.
Also, please create a new topic and don’t bump old ones.

Sorry about the bumping this topic. Also it’s returning as an instance, not a CFrame or a Position. It says so in the error.