Error with Combined Loop

Thought I would have a go at scripting today.

I’m attempting to make a part change colour every 2 seconds, then move up a bit after 10 seconds (while still changing colour) before moving back down to where it originally was.

The script is doing the first bit of it correctly, and changing the colour every 2 seconds. However, it is not doing the second part where it is meant to move up and down. I’ve had a look around the DevForum but I am still a bit unsure of what the error is.

Part = game.Workspace.Part

spawn(function()
	local i = 0
while true do
	i = i + wait()
		wait(2)
		  Part.BrickColor = BrickColor.new("Cork")
		wait(2)
		  Part.BrickColor = BrickColor.new("Medium blue")
	if i >=10 then
		 i = 0
			Part.Position = Vector3(0.51,5,-14.55)
			  wait(10)
			Part.Position = Vector3(0.51,0.5,-14.55)
		end
	end
end)

Why don’t you just make it so that the code line which moves the position idc next to the color changing script.

Never mind, gotcha.

It MUST be Vector3.new(), NOT Vector3.

1 Like

Hmm… Just changed that but it seems to still not want to move.

i+wait() will increase y by at least 1/max(30,fps) rather than the time since you last called it. So it would take 1200 seconds before i would be 10.

1 Like

Ah maybe it’s down to that then! How would I fix that?

You should be accumulating each wait, or just using a clock and figuring out the difference. The following would go wherever you want to ‘set’ the clock to zero:

local startTime = os.clock()

Then, when you want to know how much time has passed:

os.clock() - startTime

In your script, this would be:

Part = game.Workspace.Part

spawn(function()
	local startTime = os.clock()
	while true do
		wait(2)
		Part.BrickColor = BrickColor.new("Cork")
		wait(2)
		Part.BrickColor = BrickColor.new("Medium blue")
		if os.clock() - startTime >= 10 then
			startTime = os.clock()
			Part.Position = Vector3.new(0.51,5,-14.55)
			wait(10)
			Part.Position = Vector3.new(0.51,0.5,-14.55)
		end
	end
end)

I’m unclear as to whether you want to reset the timer in it’s current place though. In the code above and your original example, you reset the clock then wait 10 seconds (which means it’ll run again the next time). If that’s not what you want, you should reset the clock after waiting 10 seconds.

1 Like

Unsure if it’s a problem on my end, but that seems to change colour until it hits the part where it is meant to move up and down. When it’s meant to move up and down it freezes on blue, and sits stationary rather than moving up.

When testing code, make sure you have the output window open. You’ll see the errors there which will help you (and us) identify what’s going wrong.

I’ve updated my previous post to address the issue. As @ItzMeZeus_IGotHacked mentioned, the issue was related to not calling Vector3.new.

1 Like

Ah yes, seems to be working now. Thank you both of you!