For loop running twice

What do you want to achieve? So, i am trying to move a part in a curve using bézier curver, however, the for loop is running twice, so it is duplicating the part that the function creates and everything that the function makes contact, i tryied to read some topics, but nothing.

here is the script:

function lerp(t,a,b)
	return a*(1-t) + b*t
end

function quadBezier(t,p0,p1,p2)
	local l1 = lerp(t,p0,p1)
	local l2 = lerp(t,p1,p2)
	local quad = lerp(t,l1,l2)
	return quad
end

function rockBezier(humanoidRootPart)
	local rock = Instance.new("Part",workspace.fx)
	rock.CFrame = humanoidRootPart.CFrame * CFrame.new(0,-6,12)
	rock.Size = Vector3.new(3,3,3)
	rock.Anchored = true
	rock.CanQuery = false
	rock.CanCollide = false
	rock.CanTouch = false	
	rock.Name = "rockkkkkk"	
	
	local detect = workspace:Raycast(rock.Position + Vector3.new(0,5,0), Vector3.new(0,-10,0),params)
	
	if detect then
		rock.Material = detect.Material
		rock.Color = detect.Instance.Color
	else
		rock.Material = "Concrete"
		rock.Color = Color3.fromRGB(98,95,97)
	end

	local pointA = rock.Position
	local pointC = rock.CFrame * CFrame.new(0,0,-24).Position
	local pointB = rock.CFrame * CFrame.new(0,30,-12).Position
	
	for i = 0,1,0.01 do
		print(i) --it is printing 0 to 1 two times
		local cframe = quadBezier(i,pointA,pointB,pointC)
		rock.Position = cframe
		task.wait()
	end
	
end

Did you make this a client-sided script? I believe it comes from a specific issue with twice the execution for that case.

Current code doesn’t look indicative of any problems to why it might have executed twice, except the possibility of it being client-sided and it is placed in a container and duplicated over to the containers from players.

Try these:

local t = 0
while t <= 1 do
    print(t)
    local cframe = quadBezier(t, pointA, pointB, pointC)
    rock.Position = cframe
    t = t + 0.01
    task.wait()
end
for i = 0, 100 do  -- 100 steps from 0 to 1
    local t = i / 100
    print(t)
    local cframe = quadBezier(t, pointA, pointB, pointC)
    rock.Position = cframe
    task.wait()
end
for i = 0, 1, 0.01 do
    print(i)
    local cframe = quadBezier(i, pointA, pointB, pointC)
    rock.Position = cframe
    task.wait()
    
    if i >= 1 then  -- Exact stopping point
        break
    end
end
1 Like

no, it is a server-sided script, and i call the function inside a remote event

Could it be possible that the remote is being called twice somehow? It is still not clear what is indicating to this issue.

no, the remote event is running just one time, and being more clear, i the function is called when a hitbox touch a enemy

the touch event is not a problem

oh sorry, the problem is the hitbox actually, if i call the function when the remote event is fired, the loop runs just one time, but the line that the function have been called dellay all the others bellow

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.