Firing a part towards player

Hi, i’m brand new to this and am failing miserably. I have a new project with baseplate. I have a Sphere part, which is going to be my cannon ball. I want the ball to fire towards the nearest player. If it hits him, he dies. If it misses and hits the floor, it resets to it’s original position and fire again.

My ball has an Attachment, a script, and a VectorForce. VectorForce.RelativeTo is set to World.
This is my script so far (don’t forget i’m totally new!):

local RunService = game:GetService("RunService")
ball = script.Parent
local startPos = ball.Position
local isFiring = false
local emptyV3 = Vector3.new(0,0,0)
closest = nil

function checkHRP() --stolen from ReturnedTrue https://devforum.roblox.com/t/npc-that-runs-away-from-nearest-player/392568
    local closest 
    local closestMagnitude = 300
    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
        if player.Character then
            local hrp = player.Character:FindFirstChild("HumanoidRootPart")
            local hrpMagnitude = hrp and (ball.Position - hrp.Position).Magnitude or math.huge
            if closestMagnitude >= hrpMagnitude then
                closestMagnitude = hrpMagnitude
                closest = hrp
            end
        end
    end
    return closest
end

local function onPartTouch(otherPart)
	
	if (otherPart.Position.Y<5)		--5 is lower than my lowest platform, so I want to reset the ball to its original position
	then
		print "hit the floor"
		ball.VectorForce.Enabled = false -- stop the ball moving. next heartbeat should then start it off again
	else 
		local partParent = otherPart.Parent
		local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
		if (humanoid) then				
			humanoid.Health = 0
			ball.VectorForce.Enabled = false --kill the player and stop the ball moving
		end
	end
end

local function heartBeat(currentTime, deltaTime)
	print (ball.Position)
	print "force:"
	print (ball.VectorForce.Force)
	if (not ball.VectorForce.Enabled) then --if we're already moving the ball, don't do anything
		closest = checkHRP() -- get the nearest human
		if (closest)
		then
			print "heartbeat reset"
			ball.Position = startPos --reset the ball to its original position
			local LV = CFrame.new(ball.Position,closest.Position).LookVector *200 --think this gets a force which will send the ball towards the player,tried allsorts here.
			ball.VectorForce.Force = LV		 --apply that force to the ball		
			ball.VectorForce.Enabled = true --enable and hopefully start the ball moving
		end
	end		
end

I’ve tried with BodyForce/LineForce in place of VectorForce but it’s still not doing what I want. The balls start position is way up in the air (where i want it because i’ll have a cannon mounted on a tower there). The player is on the baseplate. I was hoping the VectorForce would shoot the ball in a downward angle towards the player, but instead it seems to head towards him in the X/Z, but goes up in the air a little bit, then plummets to the ground (gravity). The strange thing is, each time it resets, without the player moving, the ball gets fired further and further away until eventually it goes off the edge of the world and I assume gets disposed. I’d like it to be less affected by gravity, not go up in the air, and have half a chance of hitting the player if he doesn’t move. I’m fairly sure it’s the CFrame bit that isn’t doing what I think it is.
Any tips/pointers gratefully accepted.

1 Like

Hey,

This is the building support section and not everyone here scripts. I think you’ll get more people who know this in the scripting support section.

Thanks, I thought it was more physics than scripting, but i’ll try in there.

You could modify that function by returned and use Vector3:FuzzyEq() since it doesn’t perform square root operations internally, you can make a custom magnitude function and then manually square the result every iteration, and then square the root only once in the end for an optimization over several iterations.