Mouse controlled projectile, Doesnt work?

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)

Get the Player's Mouse on the client side instead, as I don’t believe you can do it server-sided that way

1 Like

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!

so should i use a remote event for that?

should i get both cframe and mouse in the client?

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)

Just get the CFrame in the client.

I do, but i was just asking if i only get the mouse from client, or both cframe and mouse

no errors, but it keeps dropping to the side for no reason?

Could we see both the local & server scripts?

sorry for the late reply, sure

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?

I believe you could use RunService.Stepped from the client? Idk why it just so happens to be deprecated up to this point

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
1 Like

what will runservice.Stepped do?

Basically it fires every 1/40’ths of a second, maybe even faster than that

I think you can use that if you want to control the projectile via the mouse?

Oh I wanted the projectile to rotate to the mouse, then fire at a fast speed on the direction of the mouse.

Is there anything I can do to fix the offset?

maxforce is not a member of bodygyro? Are you talking about max torque?

Actually supposed to be MaxTorque & not MaxForce

image

2 Likes