How to send a projectile to where the mouse clicked?

Hello, I have a projectile script that I’m trying to make go to where the player first clicked but I’m not sure how to do that, I tried sending mouse.X and mouse.Y to a script through a remote, but that didn’t work.

script.Ball.OnServerEvent:Connect(function(player,)
	_G.ZJUZNSW = player.Name
	char = player.Character
	--[[anim = char.Humanoid:LoadAnimation(script.Parent.Animation)
	anim:Play()]]
	wait(0.5)
	cannon = Instance.new("Part",game.Workspace)
	cannon.BrickColor = BrickColor.new("Bright red")
	cannon.Shape = "Ball"
	cannon.Size = Vector3.new(7,7,7)
	cannon.Material = "Neon"
	cannon.BackSurface = "Smooth"
	cannon.LeftSurface = "Smooth"
	cannon.RightSurface = "Smooth"
	cannon.TopSurface = "Smooth"
	cannon.BottomSurface = "Smooth"
	cannon.FrontSurface = "Smooth"
	cannon.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0,4,-8)
	bodyvelocity = Instance.new("BodyVelocity")
	bodyvelocity.Velocity = player.Character.Torso.CFrame.lookVector * 70 ---body velocity direction
	bodyvelocity.Parent = cannon
	bodyvelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	particle = game.ReplicatedStorage.TOOLS.FireballP
	particle2 = particle:Clone()
	particle2.Parent = cannon
	--[[dmg = script.Parent.dmg
	dmg1 = dmg:Clone()
	dmg1.Parent = cannon
	dmg1.Disabled = false]]
	wait(4)
	db = true

end)
2 Likes
  1. Do you have a local script that fires that Remote Event to the server when you detect the user input of your choice? You should be sending the target coordinates in that Remote Event as the parameter (e.g. mouse.Hit when the player clicks the left mouse button).
  2. Is this a 1 player game? If you are setting the player.Name to a static global variable then that will be overwritten by any other player that shoots after that.
  3. Are you sure you want to use a Body Velocity? If you send the mouse.Hit in the Remote Event and then use a Body Force you can send the bullet at the World Coordinates of the mouse.Hit CFrame.
  4. Are you asking for help on how to deal the damage as well or are you able to figure that out once you know how to fire the “cannon”?

You should use mouse.Target and TweenService in my opinion. I see that you are pretty good in scripting so I will let you try making it.

I have a local script that fires and sends mouse.Hit when button1 goes down, yes I know that it will be overwritten after another play shoots it. I can make it so that it deals damage, only thing I lack experience is, is in bodymovers.

script.Ball.OnServerEvent:Connect(function(player,mouse)
	_G.ZJUZNSW = player.Name
	char = player.Character
	--[[anim = char.Humanoid:LoadAnimation(script.Parent.Animation)
	anim:Play()]]
	wait(0.5)
	FireBall = Instance.new("Part",game.Workspace)
	FireBall.BrickColor = BrickColor.new("Bright red")
	FireBall.Shape = "Ball"
	FireBall.Size = Vector3.new(7,7,7)
	FireBall.Material = "Neon"
	FireBall.BackSurface = "Smooth"
	FireBall.LeftSurface = "Smooth"
	FireBall.RightSurface = "Smooth"
	FireBall.TopSurface = "Smooth"
	FireBall.BottomSurface = "Smooth"
	FireBall.FrontSurface = "Smooth"
	FireBall.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0,4,-8)
	BodyForce = Instance.new("BodyForce")
	BodyForce.Force = mouse ---error is here
	BodyForce.Parent = FireBall
	particle = game.ReplicatedStorage.TOOLS.FireballP
	particle2 = particle:Clone()
	particle2.Parent = FireBall
	--[[dmg = script.Parent.dmg
	dmg1 = dmg:Clone()
	dmg1.Parent = FireBall
	dmg1.Disabled = false]]
	wait(4)
	db = true

end)

mouse is mouse.Hit
this returns an error

 bad argument #3 to 'Force' (Vector3 expected, got CFrame)
1 Like

The error told you what the problem is. It wants a Vector3 value, not a CFrame. Simply identify the Position of the CFrame and you are good to go.

BodyForce.Force = mouse.Position
2 Likes

Thanks! It worked :grinning: :smiley:

1 Like