I’ve been trying to make a projectile system, and I want to get the player’s mouse to shoot where ever the player’s mouse. However, It won’t work for some reason and gives the error:
18:26:09.405 Players.k1212ss.Backpack.Piercing Shot.Server:15: attempt to index nil with ‘Hit’
this is a server script, any ideas why this script had the error?
local TweenService = game:GetService("TweenService")
local part = nil
local part2 = nil
script.Parent.Slash.OnServerEvent:Connect(function(plr,humanoid)
local char = plr.Character
local mouse = plr:GetMouse()
local e = game.ReplicatedStorage.Auras.NeonWhiteArrow:Clone()
e.Parent = game.Workspace.Projectiles
e.CFrame = CFrame.new(char.HumanoidRootPart.CFrame.Position + (char.HumanoidRootPart.CFrame.LookVector*2))
e.Orientation = char.HumanoidRootPart.Orientation
e:SetNetworkOwner(plr)
local velo = e.BodyForce
local pushForce = 500
velo.Force = (mouse.Hit.p - e.Position).Unit * part:GetMass() * pushForce
wait(1)
e:Destroy()
end)
You can’t get the player’s mouse from the server-side. So if you need to get the CFrame of the player’s mouse, you need to add a remote function for the client to return the mouse.Hit back to the server. Hope this helps!
Just pass the Player’s Mouse Hit Position on the client-side, and receive it from the server-side that way so yes (I thought you already knew about RemoteEvents though)
I just noticed it works, but goes a bit down, so it looks like its going down, how do i fix this? oh and is there a way to rotate to the player’s mouse, and control the speed?
For rotating to the player’s mouse, maybe add a BodyGyro to the HumanoidRootPart of the character, and set the CFrame of the BodyGyro toward the mouse? Hope this helps!
local Debris = game:GetService("Debris")
local BodyGyro = Instance.new("BodyGyro") -- Make a body gyro
BodyGyro.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
BodyGyro.CFrame = CFrame.new(HumanoidRootPart.Position,MouseHit.Position) -- Define HumanoidRootPart and MouseHit yourself
BodyGyro.Parent = HumanoidRootPart
Debris:AddItem(BodyGyro,1) -- You set the time the body gyro will be destroyed