Temperature Script Help

H! I have a problem while creating a Fivem Script that calculates temperature according to the month and a random generator. When i ensure the script in the Fivem server, i have an error.
CODE:

local temperature
local monthNumberR
local monthStringG
local mustSnow
local probTempHigh
local probTempLow
local showTemperature = true
local displayY

Citizen.CreateThread(function()
	while true do
		Wait(1)
		
		if showTemperature == true then
			CalculateTemperature()
			displayY = temperature.."°C"
		end
		
		SetTextFont(0)
		SetTextProportional(1)
		SetTextScale(0.40, 0.40)
		SetTextColour(255, 255, 255, 255)
		SetTextDropshadow(0, 0, 0, 0, 255)
		SetTextEdge(1, 0, 0, 0, 255)
		SetTextDropShadow()
		SetTextOutline()
		SetTextRightJustify(true)
		SetTextWrap(0,0.95)
		SetTextEntry("STRING")
		
		AddTextComponentString(displayY)
		DrawText(0.5, 0.01)
	end
end)

function CalculateTemperature()
    monthNumberR = getClockMonth()

    --MONTH STRING CONVERTER
    if monthNumberR == 0 then
		monthStringG = "JAN"
	elseif monthNumberR == 1 then
		monthStringG = "FEB"
	elseif monthNumberR == 2 then
		monthStringG = "MAR"
	elseif monthNumberR == 3 then
		monthStringG = "APR"
	elseif monthNumberR == 4 then
		monthStringG = "MAY"
	elseif monthNumberR == 5 then
		monthStringG = "JUN"
	elseif monthNumberR == 6 then
		monthStringG = "JUL"
	elseif monthNumberR == 7 then
		monthStringG = "AUG"
	elseif monthNumberR == 8 then
		monthStringG = "SEP"
	elseif monthNumberR == 9 then
		monthStringG = "OCT"
	elseif monthNumberR == 10 then
		monthStringG = "NOV"
	elseif monthNumberR == 11 then
		monthStringG = "DEC"
	end

    --MONTH PROB TEMP HIGH/LOW CALCULATOR
    if monthStringG == "DEC" or monthStringG == "GEN" or monthStringG == "FEB" then
        probTempHigh = 3
        probTempLow = 97
    else if monthStringG == "MAR" or monthStringG == "APR" or monthStringG == "MAY" then
        probTempHigh = 20
        probTempLow = 80
    else if monthStringG == "JUN" or monthStringG == "JUL" then
        probTempHigh = 95
        probTempLow = 5
    else if monthStringG == "AUG" then
        probTempHigh = 70
        probTempLow = 30
    else if monthStringG == "SEP" then
        probTempHigh = 50
        probTempLow = 50
    else if monthStringG == "OCT" then
        probTempHigh = 40
        probTempLow = 60
    else if monthStringG == "NOV" then
        probTempHigh = 30
        probtempLow = 70
    end

    --TEMPERATURE CALCULATOR
    local random = math.random(0,100)
    local random2 = math.random(0,100)
    local random3SNOW = math.random(0,100)

    if random <= probTempHigh then
        if probTempHigh <= 10 then
            temperature = math.random(22,26)
        else if probTempHigh <= 30 then
            temperature = math.random(27,29)
        else if probTempHigh <= 70 then
            if random2 <= 50 then
                temperature = math.random(30,33)
            else
                temperature = math.random(22,30)
            end
        else
            if random2 <=50 then
                temperature = math.random(33,35)
            else
                temperature = math.random(36,40)
            end
        end
    else
        if probTempLow <= 10 then
            temperature = math.random(16,22)
        else if probTempLow <= 30 then
            temperature = math.random(13,16)
        else if probTempLow <= 70 then
            if random2 <= 50 then
                temperature = math.random(9,13)
            else
                temperature = math.random(7,9)
        else
            if random2 <=50 then
                temperature = math.random(5,7)
            else
                temperature = math.random(0,5)
                
                --SNOW CALCULATOR
                if random3SNOW <= 50 then
                    mustSnow = true
                else
                    mustSnow = false
                end
            end
        end
    end



end





--UTILITIES
function textNotify(text)
    SetNotificationTextEntry("STRING")
    AddTextComponentString(text)
    DrawNotification(true, true)
end

Thank you

Hello, show us the error then. It’ll be better to help you :laughing:

1 Like

now i fixed the error, an end was missing after an if, but when i run it doesn’t show anything on the screen

Try to set the font as 1 or 4

1 Like

unfortunately still nothing

I modified a little the code but now i have this error:

Error parsing script @SeasonScript/client/temperature.lua in resource @SeasonScript/client/temperature.lua:115: 'end' expected (to close 'if' at line 100) near else

but the if is closed at line 139.
The code is:

local temperature
local monthNumberR
local monthStringG
local mustSnow
local probTempHigh
local probTempLow
local showTemperature = true
local displayY

Citizen.CreateThread(function()
	while true do
		Wait(1)
		
		if showTemperature == true then
			CalculateTemperature()
			displayY = temperature.."°C"
		end
		
		SetTextFont(0)
		SetTextProportional(1)
		SetTextScale(0.40, 0.40)
		SetTextColour(255, 255, 255, 255)
		SetTextDropshadow(0, 0, 0, 0, 255)
		SetTextEdge(1, 0, 0, 0, 255)
		SetTextDropShadow()
		SetTextOutline()
		SetTextRightJustify(true)
		SetTextWrap(0,0.95)
		SetTextEntry("STRING")
		
		AddTextComponentString(displayY)
		DrawText(0.5, 0.01)

        textNotify(displaY)
	end
end)

function CalculateTemperature()
    monthNumberR = getClockMonth()

    --MONTH STRING CONVERTER
    if monthNumberR == 0 then
		monthStringG = "JAN"
	elseif monthNumberR == 1 then
		monthStringG = "FEB"
	elseif monthNumberR == 2 then
		monthStringG = "MAR"
	elseif monthNumberR == 3 then
		monthStringG = "APR"
	elseif monthNumberR == 4 then
		monthStringG = "MAY"
	elseif monthNumberR == 5 then
		monthStringG = "JUN"
	elseif monthNumberR == 6 then
		monthStringG = "JUL"
	elseif monthNumberR == 7 then
		monthStringG = "AUG"
	elseif monthNumberR == 8 then
		monthStringG = "SEP"
	elseif monthNumberR == 9 then
		monthStringG = "OCT"
	elseif monthNumberR == 10 then
		monthStringG = "NOV"
	elseif monthNumberR == 11 then
		monthStringG = "DEC"
	end

    --MONTH PROB TEMP HIGH/LOW CALCULATOR
    if monthString == "DEC" or monthString == "GEN" or monthString == "FEB" then
        probTempHigh = 3
        probTempLow = 97
    else if monthString == "MAR" or monthString == "APR" or monthString == "MAY" then
        probTempHigh = 20
        probTempLow = 80
    else if monthString == "JUN" or monthString == "JUL" then
        probTempHigh = 95
        probTempLow = 5
    else if monthString == "AUG" then
        probTempHigh = 70
        probTempLow = 30
    else if monthString == "SEP" then
        probTempHigh = 50
        probTempLow = 50
    else if monthString == "OCT" then
        probTempHigh = 40
        probTempLow = 60
    else if monthString == "NOV" then
        probTempHigh = 30
        probtempLow = 70
    end

    --TEMPERATURE CALCULATOR
    local random = math.random(0,100)
    local random2 = math.random(0,100)
    local random3SNOW = math.random(0,100)

    if random <= probTempHigh then
        if probTempHigh <= 10 then
            temperature = math.random(22,26)
        else if probTempHigh <= 30 then
            temperature = math.random(27,29)
        else if probTempHigh <= 70 then
            if random2 <= 50 then
                temperature = math.random(30,33)
            else
                temperature = math.random(22,30)
            end
        else
            if random2 <=50 then
                temperature = math.random(33,35)
            else
                temperature = math.random(36,40)
            end
        end
    else
        if probTempLow <= 10 then
            temperature = math.random(16,22)
        else if probTempLow <= 30 then
            temperature = math.random(13,16)
        else if probTempLow <= 70 then
            if random2 <= 50 then
                temperature = math.random(9,13)
            else
                temperature = math.random(7,9)
        else
            if random2 <=50 then
                temperature = math.random(5,7)
            else
                temperature = math.random(0,5)
                
                --SNOW CALCULATOR
                if random3SNOW <= 50 then
                    mustSnow = true
                else
                    mustSnow = false
                end
            end
        end
    end



end





--UTILITIES
function textNotify(text)
    SetNotificationTextEntry("STRING")
    AddTextComponentString(text)
    DrawNotification(true, true)
end
1 Like

replace line 143 from “end” to “end)”

should solve it

1 Like

Ah ok now i’m updating windows, after that, i will try

“else if” syntax is not correct, right syntax for lua is “elseif”

1 Like

OK thank you. being used to c ++, I often forget all other syntaxes

Now i have this error:

SCRIPT ERROR: @SeasonScript/client/temperature.lua:39: attempt to call a nil value (global 'getClockMonth')
fn (@SeasonScript/client/temperature.lua:15)

You are calling a function that doesn’t exist

1 Like

I found that in Fivem Docs

EDIT:
The function is GetClockMonth() not getClockMonth()

I modified that and now it says that at line 97 (if random <= probTempHigh then) i’m comparing number with nil

Are you sure that probTempHigh is correctly define?

1 Like

At first I initialize it with local probTempHigh, then I assign it to a numeric value between 0 and 100 (e.g. probTempHigh = 3) and then I do that if

CODE:

local temperature
local monthNumberR
local monthStringG
local mustSnow
local probTempHigh
local probTempLow
local showTemperature = true
local displayY

Citizen.CreateThread(function()
	while true do
		Wait(1)
		
		if showTemperature == true then
			CalculateTemperature()
			displayY = temperature.."°C"
		end
		
		SetTextFont(0)
		SetTextProportional(1)
		SetTextScale(0.40, 0.40)
		SetTextColour(255, 255, 255, 255)
		SetTextDropshadow(0, 0, 0, 0, 255)
		SetTextEdge(1, 0, 0, 0, 255)
		SetTextDropShadow()
		SetTextOutline()
		SetTextRightJustify(true)
		SetTextWrap(0,0.95)
		SetTextEntry("STRING")
		
		AddTextComponentString(displayY)
		DrawText(0.5, 0.01)

        textNotify(displaY)
	end
end)

function CalculateTemperature()
    monthNumberR = GetClockMonth()

    --MONTH STRING CONVERTER
    if monthNumberR == 0 then
		monthStringG = "JAN"
	elseif monthNumberR == 1 then
		monthStringG = "FEB"
	elseif monthNumberR == 2 then
		monthStringG = "MAR"
	elseif monthNumberR == 3 then
		monthStringG = "APR"
	elseif monthNumberR == 4 then
		monthStringG = "MAY"
	elseif monthNumberR == 5 then
		monthStringG = "JUN"
	elseif monthNumberR == 6 then
		monthStringG = "JUL"
	elseif monthNumberR == 7 then
		monthStringG = "AUG"
	elseif monthNumberR == 8 then
		monthStringG = "SEP"
	elseif monthNumberR == 9 then
		monthStringG = "OCT"
	elseif monthNumberR == 10 then
		monthStringG = "NOV"
	elseif monthNumberR == 11 then
		monthStringG = "DEC"
	end

    --MONTH PROB TEMP HIGH/LOW CALCULATOR
    if monthString == "DEC" or monthString == "GEN" or monthString == "FEB" then
        probTempHigh = 3
        probTempLow = 97
    elseif monthString == "MAR" or monthString == "APR" or monthString == "MAY" then
        probTempHigh = 20
        probTempLow = 80
    elseif monthString == "JUN" or monthString == "JUL" then
        probTempHigh = 95
        probTempLow = 5
    elseif monthString == "AUG" then
        probTempHigh = 70
        probTempLow = 30
    elseif monthString == "SEP" then
        probTempHigh = 50
        probTempLow = 50
    elseif monthString == "OCT" then
        probTempHigh = 40
        probTempLow = 60
    elseif monthString == "NOV" then
        probTempHigh = 30
        probtempLow = 70
    end

    --TEMPERATURE CALCULATOR
    local random = math.random(0,100)
    local random2 = math.random(0,100)
    local random3SNOW = math.random(0,100)

    if random <= probTempHigh then
        if probTempHigh <= 10 then
            temperature = math.random(22,26)
        elseif probTempHigh <= 30 then
            temperature = math.random(27,29)
        elseif probTempHigh <= 70 then
            if random2 <= 50 then
                temperature = math.random(30,33)
            else
                temperature = math.random(22,30)
            end
        else
            if random2 <=50 then
                temperature = math.random(33,35)
            else
                temperature = math.random(36,40)
            end
        end
    else
        if probTempLow <= 10 then
            temperature = math.random(16,22)
        elseif probTempLow <= 30 then
            temperature = math.random(13,16)
        elseif probTempLow <= 70 then
            if random2 <= 50 then
                temperature = math.random(9,13)
            else
                temperature = math.random(7,9)
            end
        else
            if random2 <=50 then
                temperature = math.random(5,7)
            else
                temperature = math.random(0,5)
                
                --SNOW CALCULATOR
                if random3SNOW <= 50 then
                    mustSnow = true
                else
                    mustSnow = false
                end
            end
        end
    end
end





--UTILITIES
function textNotify(text)
    SetNotificationTextEntry("STRING")
    AddTextComponentString(text)
    DrawNotification(true, true)
end

before your if statement, print probTempHigh to see if it’s defined or not

I added this lines:

    textNotify("probTempHigh:"..tostring(probTempHigh))
    textNotify("probTempLow:"..tostring(probTempLow))

function textNotify(text)
    SetNotificationTextEntry("STRING")
    AddTextComponentString(text)
    DrawNotification(true, true)
end

and now it prints

probTempHigh:nil
probTempLow:nil

EDIT:
I modified the initialization of the variables,

local probTempHigh = 0
local probTempLow = 0

and now it works!

1 Like