Roblox code doesnt work after running one time

So, i was basically doing a spinner but the code decided to not work and only run one time and break

Heres the code:

local dot1 = script.Parent:WaitForChild("dot1")
local dot2 = script.Parent:WaitForChild("dot2")
local dot3 = script.Parent:WaitForChild("dot3")
local dot4 = script.Parent:WaitForChild("dot4")
local dot4 = script.Parent:WaitForChild("dot4")
local ts = game:GetService("TweenService")
local pos1 = dot1.Position
local pos2 = dot2.Position
local pos3 = dot3.Position
local pos4 = dot4.Position
local anc1 = dot1.AnchorPoint
local anc2 = dot2.AnchorPoint
local anc3 = dot3.AnchorPoint
local anc4 = dot4.AnchorPoint

function tween(obj,time,props)
	ts:Create(obj,TweenInfo.new(time,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut),props):Play()
end

function spin()
	tween(dot1,0.3,{Position = pos3,AnchorPoint = anc3})
	tween(dot2,0.3,{Position = pos1,AnchorPoint = anc1})
	tween(dot3,0.3,{Position = pos4,AnchorPoint = anc4})
	tween(dot4,0.3,{Position = pos2,AnchorPoint = anc2})
	wait(0.3)
	local pos1 = dot1.Position
	local pos2 = dot2.Position
	local pos3 = dot3.Position
	local pos4 = dot4.Position
	local anc1 = dot1.AnchorPoint
	local anc2 = dot2.AnchorPoint
	local anc3 = dot3.AnchorPoint
	local anc4 = dot4.AnchorPoint
end

while true do
	spin()
	wait()
end

Hirearchy:

image

Heres a clip of it happening:

Please just help me fix this.

put

local pos1 = dot1.Position
local pos2 = dot2.Position
local pos3 = dot3.Position
local pos4 = dot4.Position
local anc1 = dot1.AnchorPoint
local anc2 = dot2.AnchorPoint
local anc3 = dot3.AnchorPoint
local anc4 = dot4.AnchorPoint

at the top in spin()

1 Like

Oh wait, i actually figured it out tho thanks for the help
i just used local inside the function which doesnt override the global one

1 Like

This is an interesting way of doing it, but it might be the fact that you’re repeatedly calling the tween without waiting for it to finish, and you’re reassigning variables instead of changing the value.

I recommend doing some kind of tween.Completed. This is different from how I’d do it in its entirety, but based on your current code, I’d write something like this:

function tween(obj, time, props)
    local t1 = ts:Create(obj,TweenInfo.new(time,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut),props)
    t1:Play()
    return t1
end

function spin()
    -- the first 3 tweens
    local finalTween = tween(dot4, 0.3, {Position = pos2, AnchorPoint = anc2})
    task.wait(0.3)

    -- most important part
    -- don't use local
    pos1 = dot1.Position
    -- rest of vars like this

    finalTween.Completed:Wait()
    spin()
end

spin()