Tween Doesn't Work Properly

Hello,

So, I created a script which would tween the terrain color depending on if its day or night, but it doesn’t work properly. First off, the grass doesn’t change once it reaches 6 hours. Second off, once it becomes night, it turns green for a split second and tweens to be blue, which is weird. I think this is a visual glitch, since it tweens from green to blue once it becomes night. Any help will be appreciated. The code is as follows:

local terrain = game.Workspace.Terrain
local value = "1"

local daynight = true

print("gotpastcode3")

local colorValue = Instance.new("Color3Value")
colorValue.Value = terrain:GetMaterialColor("Grass")
colorValue.Parent = script.Parent
print("gotpastcode2")

colorValue:GetPropertyChangedSignal("Value"):connect(function()
	terrain:SetMaterialColor("Grass", colorValue.Value)
end)
print("gotpastcode1")

terrain:SetMaterialColor("Grass", Color3.fromRGB(91, 122, 380))

local tweenInfo = TweenInfo.new(30, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
local tweenInfo2 = TweenInfo.new(30, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
local Night = game:GetService("TweenService"):Create(colorValue, tweenInfo, {Value = Color3.fromRGB(91, 122, 380 )})
local Day = game:GetService("TweenService"):Create(colorValue, tweenInfo2, {Value = Color3.fromRGB(110, 148, 70)})
print("got past code")



while true do
	Time = game.Lighting.TimeOfDay
	TimeOfDay = Time
	hour = TimeOfDay:split(':')
	hours = tonumber(hour[1])
	if hours >= 17 and hours <= 20 then
		print("17-20 hours")
		if daynight == false then
			Night:Play()
			print("Tweened")
			daynight = true
			wait(0.1)
		end
	end
	if hours >= 6 and hours <= 9 then
		print("6-9 hours")
		if daynight == true then
			Day:Play()
			print("Tweened")
			daynight = false
			wait(0.1)
		end
	end
	print("Looped!")
	print(hours)
	wait(0.1)
end

EDIT 1- The loop works, since it prints “looped”, which means that the tweening is not working

1 Like

First of all, you don’t need two different TweenInfo.

As for the tweening issue, try putting the function that sets the material color based on the color value in another script or inside the while loop.

When the loop is running forever, I don’t know if a function connected to an event works exactly as wanted.

2 Likes

Nope, after testing, it still doesn’t work

1 Like

Something else interesting:
I set the value in studio to be 91, 122, 255 since it starts in the night, and immediately it turns green for some reason. Any thoughts about this?

1 Like

It prints on every hour and the 21 to 25 is just to keep in check just in case the code doesn’t tween the first time, it would the second.

print(hours)

You have daynight set to true initially, so is that why the day code runs first?

1 Like

In order for the daynight to even be checked it would have to be between 6-9 or 21-25 hours

What’s the TimeOfDay variable declared? It seems like you’re declaring a global variable inside of a scope, but then again, it may be outside of the code you provided.

1 Like

TimeOfDay = Time = game.Lighting.TimeOfDay

The loop checks the time every time the loop repeats so the time gets tracked in real time

You can’t declare a global variable inside of a scope like a loop. Add a local in front of the variable.

1 Like

Don’t think that’s the problem, though, because the time still gets declared in the output and the grass still tweens

The tweens work after that anyways. It’s just the grass tweening once the client joins

Instead, I’ll try starting the game at 6 hours, so the tweening problem won’t occur, and I’ll continue to watch to make sure it works consistently. Thanks for your help!

1 Like