Attempt to compare number and string

Hello,

After trying many times, I’ve tried using numberstrings and stringvalues, but no luck. The error and script are as follows (without indentation):

Workspace.Terrain.GrassHandler:27: attempt to compare number and string

local terrain = game.Workspace.Terrain

local Time = game.Lighting.TimeOfDay

local value = "1"

local daynight = tostring(value)

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")

local tweenInfo = TweenInfo.new(60, 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, tweenInfo, {Value = Color3.fromRGB(110, 148, 70)})

print("got past code")

while true do

local TimeOfDay = Time

local hours = TimeOfDay:split(':')[1]

if hours >= 21 and hours <= 24 and daynight == 0 then

Night:Play()

print("Tweened")

daynight = 1

wait(0.1)

end

if hours >= 6 and hours <= 9 and daynight == 1 then

Day:Play()

print("Tweened")

daynight = 0

wait(0.1)

end

print("Looped!")

wait(0.1)

end

Any help will be greatly appreciated.
Thanks,

R3M1X
1 Like

A couple of things:

  • Please use ```lua to hold your code on the forums. It makes it easier to see.
  • Since you didn’t give any information on where the error is occurring, my best guess is that the script is trying to compare “hours” (which is a string) to 6 or 9. You might wanna take a look at that.
1 Like

you are changing your number value to a string value when you say: “local daynight = tostring(value)”

So when you try to compare it with a number " if hours >= 6 and hours <= 9 and daynight == 1 then", it will throw an error. Try to keep it a number value?

Just say: local value = 1 without the quotations to keep it a number
edit: also to change a string value “1” to a number say tonumber(value)

4 Likes

The first line:
Workspace.Terrain.GrassHandler:27: attempt to compare number and string

It still won’t fix the problem, I can’t think of anything else that would cause it

Have you replaced ALL of the instances of hours (except for the declaration) with tonumber(hours)? I made the changes and the script appears to be running fine.

Edit: Also, is there any particular reason that you’re changing value between 1 and 0 instead of just using a boolean?

1 Like

Actually just got it working. Haven’t thought of using a boolean

Can you send me your version of the script please? I’m not sure if the way I did it was the most efficient way.

It’s not the most efficient way, but I’m taking care of a couple of things right now so I can’t really help. But if you wanna try it yourself:

  1. Avoid wait(). It’s typically seen as bad practice. If you need an infinite loop, consider using RenderStepped or similar instead, and check to make sure that the time passed is however long you need it to be (by default, RenderStepped runs every frame, which goes up to 60 normally).
  2. Replace your “value” with a boolean. Unless you’re doing something fancy, you don’t need to have a string value.
1 Like