Script doesn't work!

I make core game and confronted with problem that my script doesn’t work.
When heatlevel value == 1,2,3 or 4 temp value needs to rise but it’s value 0, nothing in output.
robloxapp-20230316-1916068.wmv (122.6 KB)
Video with that problem ^
Help please!

local items = script.Parent

	if items.HeatLevel.Value == 1 then
		items.Temp.Value += math.random(2,15)
	elseif items.HeatLevel.Value == 2 then
		items.Temp.Value += math.random(7,30)
	elseif items.HeatLevel.Value == 3 then
		items.Temp.Value += math.random(12,54)
	elseif items.HeatLevel.Value == 4 then
		items.Temp.Value += math.random(15,69)
	end

I think the script isnt in a loop, it all runs straight away, even when you get to the clicking button.

while task.wait(1) do
	if items.HeatLevel.Value == 1 then
		items.Temp.Value += math.random(2,15)
	elseif items.HeatLevel.Value == 2 then
		items.Temp.Value += math.random(7,30)
	elseif items.HeatLevel.Value == 3 then
		items.Temp.Value += math.random(12,54)
	elseif items.HeatLevel.Value == 4 then
		items.Temp.Value += math.random(15,69)
	end
end
local items = script.Parent
local Heat = items.Heat
local Temp = {
   [1] = function()
        items.Temp.Value += math.random(2,15)
   end,
   [2] = function()
		items.Temp.Value += math.random(7,30)
   end,
   [3] = function()
		items.Temp.Value += math.random(12,54)
   end,
   [4] = function()
		items.Temp.Value += math.random(15,69)
   end,
}
Temp[Heat.Value]()
Heat.Changed:Connect(function(Value)
   Temp[Value]()
end)

You can do this with your code, @weakroblox35’s code might be better but this is just another method:

local usableValues = {1, 2, 3, 4} -- values you can use
local values = {{2, 15},{7, 30},{12, 54},{15, 69}} -- stored values to access

function add(num)
    if usableValues[num] then -- if the value can be used
        items.Temp.Value += math.random(values[num][1], values[num][2]) -- adds random by using stored values
    end
end



-- whatever here:

add(items.HeatLevel.Value)

However if you want more far results, use Random.new()

local random = Random.new() -- Random (Instance? It could just be a table)

function add(num)
    if usableValues[num] then -- if the value can be used
        items.Temp.Value += random:NextInteger(values[num][1], values[num][2]) -- adds random by using stored values
    end
end


First:
Use tables as others have said
Second:
It doesn’t run because you aren’t listening for the Changed event, try:

local items = script.Parent

items.Changed:Connect(function()
	if items.HeatLevel.Value == 1 then
		items.Temp.Value += math.random(2,15)
	elseif items.HeatLevel.Value == 2 then
		items.Temp.Value += math.random(7,30)
	elseif items.HeatLevel.Value == 3 then
		items.Temp.Value += math.random(12,54)
	elseif items.HeatLevel.Value == 4 then
		items.Temp.Value += math.random(15,69)
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.