Bezier curve soccer shooting not working well

I am doing a roblox soccer game and i am currently trying to make a shooting system using Bezier curves.
The problem is that the ball kinda moves in a bizarre way before reaching destination and sometimes bounces everywhere and falls of the game.
Everything else in th scripts works ,here is the script:

local event = game:GetService("ReplicatedStorage"):FindFirstChild("BezierCurve")
event.OnServerEvent:Connect(function(player,duration,Look,position,MousePosition)
	if duration > 4 then duration = 4
	end
	local a = Instance.new("Part")
	local P1A = Instance.new("Part")
	local b = Instance.new("Part")
	a.Anchored = true
	P1A.Anchored = true
	b.Anchored = true
	a.CanCollide = false
	b.CanCollide = false
	P1A.CanCollide = false
	local ball = game.Workspace:FindFirstChild("Ball1")
	ball.Transparency = 0
	ball.a.Transparency =0
	ball.b.Transparency =0
	ball.c.Transparency =0
	ball.d.Transparency =0
	ball.f.Transparency =0
	a.Position = position + Vector3.new(6,0,0)
	local tDistanceY = duration* 20
	local X = MousePosition.x
	local Y = MousePosition.y
	local Z = MousePosition.z
	local bPosition = Vector3.new(X,Y,Z)
	b.Position = bPosition
	print("reached position mouse")
	P1A.Position = (bPosition + a.Position)/2 + Vector3.new(0,tDistanceY,0)
	print("mid position")
	local distance = (bPosition - a.Position).Magnitude
	
	
	local lerptime = (0.00525 * distance)/duration
	local function curve(t,P0,P1,P2)
		local A = P0:Lerp(P1,t)
		local B = P1:Lerp(P2,t)
		return A:Lerp(B,t)
		end
		for i = 0,1,0.045 do
			local Bezier = curve(i,position,P1A.Position,bPosition)
		ball.Position = Bezier
		
		
		print("bezier")
		local waitime =  lerptime*0.045
		wait(waitime)
		print("wait")
		if i == 1 then
			a:Destroy()
			b:Destroy()
			P1A:Destroy()
		end
		
		
	end
	end)

Here is what happens :
robloxapp-20230515-1745546.wmv (3.6 MB)

I have tried looking tutorials on beziercurves in the devforum and youtube but i still can’t fix it .
I also tried changing the collisions and the starting position.

i found that the reason is that there is the starting point and every time it moves it goes to the starting point and then the new one for an unknown reason but not only this sometimes it really moves randomly .

make sure to mark it as a solution

Now i really fixed by replacing the wait by task.wait() with nothing in brackets but i still want it to have a certain time

1 Like