Projectile accuracy is kinda screwy + hitting the player?

ACCURACY

if closestPlayer ~= nil then

			--play animation
			local attackAnim = Instance.new("Animation")
			attackAnim.AnimationId = "rbxassetid://8723703513"
			local loadAttackAnim = persona.AnimationController:LoadAnimation(attackAnim)
			loadAttackAnim:Play()

			--waiting for animation event
			loadAttackAnim:GetMarkerReachedSignal("attackKeyFrame"):Connect(function()

				--when we reach this point in the animation, ATTACK!
				--creating fireball
				local fireBall = game.ServerStorage.personaFireBallAttack:Clone()
				fireBall.Position = persona.HumanoidRootPart.Position
				fireBall.Parent = persona


				--setting cframe.lookat
				fireBall.CFrame = CFrame.lookAt(fireBall.Position, closestPlayer.Character.HumanoidRootPart.Position)


				--creating body velocity
				local velocity = Instance.new("BodyVelocity")
				velocity.MaxForce = Vector3.new(1000, 1000, 1000)
				velocity.Velocity = fireBall.CFrame.LookVector * speed
				velocity.Parent = fireBall


				--playing attack sound
				fireBall.shotSound:Play()

			end)

		end

Closest player is the player being targeted.
Everything in this script works, except even when the target stands still, the fireball always seems to miss by a stud or two. Why is this? A problem with the projectile part itself? The CFrame.lookAt? The body velocity?
NOTE: The projectile is cancollide false, and thats the only messing with it I did, it’s also a sphere.

HITTING THE PLAYER

script.Parent.Touched:Connect(function(hit)

	--if it was a humanoid, and NOT the player
	if hit.Parent:FindFirstChild("Humanoid") and not script:FindFirstAncestor(hit.Parent) then

		Explosion(hit, true) --fire explosion script, with isPlayer checked to TRUE


	elseif hit:FindFirstAncestor(game.Workspace.Map.Name) then --otherwise, if it is part of the map

		Explosion(hit, false) --fire explosion script, with isPlayer checked to FALSE
	end

	--otherwise, do nothing
end)

This is the damage script inside a projectile. The projectile is the child of the object firing it, which is the child of the player. Because of this, I assumed that if it hit the player, hit.Parent would be the player’s character. So, I check to see if the bullet can find hit.Parent (the player’s character) as its ancestor. If it is the player who fired the shot, it shouldn’t work, due to the “not”. While there are no errors, this doesn’t seem to do anything, and when it accidentally hits the player shooting the projectile, it does damage anyways.

You should try making the fireball slightly offset from the player…

A few things to look at:

Position doesn’t respect welds. This means if your fireball is welded together, setting the position changes how it’s welded together.

I’d just remove that line and change the CFrame line to this:

fireBall.CFrame = CFrame.lookAt(persona.HumanoidRootPart.Position, closestPlayer.Character.HumanoidRootPart.Position)

You probably shouldn’t parent the fireball to the character because it can mess with the camera and character controller in some cases.

If it seems to be working fine it’s probably alright though.


Not sure what isn’t working with the hit code. I personally use the IsDescendantOf function in this case,
because it’s a bit more readable:

script.Parent.Touched:Connect(function(hit)

	--if it was a humanoid, and NOT the player
	if hit.Parent:FindFirstChild("Humanoid") and not script:IsDescendantOf(hit.Parent) then

		Explosion(hit, true) --fire explosion script, with isPlayer checked to TRUE


	elseif hit:IsDescendantOf(game.Workspace.Map.Name) then --otherwise, if it is part of the map

		Explosion(hit, false) --fire explosion script, with isPlayer checked to FALSE
	end

	--otherwise, do nothing
end)

Sorry, this reply is… extremely late. Either way, the “IsDescendantOf” appears to have worked, but the other thing with the accuracy didn’t seem to.

Some notes to possibly help you narrow down the problem, if you’re still willing to help:
-Changing the size of the part makes it shoot towards the floor for some reason
-I changed the Massless property to true; this changed nothing (and didn’t alter the strange falling-toward-the-floor of the first note)
-It’s parented to the persona, which is being animated… would this by any chance alter it’s position slightly (Although it isn’t part of the animation, obviously isn’t rigged to the character, or anything like that)?

This is probably because of gravity. You can fix this by increasing the max force:

velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

I think massless only makes things massless relative to their assembly. For example, if you had a car and added a massless part it wouldn’t effect the motion of the car. Massless objects need to be connected into an assembly for that to work though.

I’m not sure about this. (To avoid this, maybe use an ObjectValue instead?)


Edit:

It doesn’t change the speed. A BodyVelocity uses force to make an object go at a certain velocity, so increasing max force just makes the BodyVelocity use more force to reach it’s target velocity.

If you just want to cancel gravity, you can also do:

bodyVelocity.Force = bodyVelocity.Force + Vector3.new(0, Workspace.Gravity*fireBall.AssemblyMass, 0)

After setting the force to 1000, 1000, 1000.

Will this alter the speed of it at all, or just possibly fix my issue?

If it does change the speed, how can I compensate for this?

1 Like