Setting number issues

The issue where is setting the number. I’m making a temperature system. I have a script that’s in serverscriptservice and modulescript is under script.
Screenshot 2023-01-15 104523

– ServerScriptService (TemperatureRemote) –

local Temp = require(script.TemperatureModule)
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local playertemp = Temp.GetTemperature(player)
	print(playertemp)
	
	wait(5)
	Temp.SetTemperature(player, 10)
	print(Temp.GetTemperature(player))
end)

– ModuleScript (TemperatureModule) –

local Temp = {}

local function isValidPlayer(player)
	return player and player:IsA("Player")
end

local function NewTemperature(player)
	local char = player.Character or player.CharacterAdded:Wait()
	local Temperature = Instance.new("NumberValue")
	Temperature.Name = "Temperature"
	Temperature.Value = 15
	Temperature.Parent = char
end

function Temp.GetTemperature(player)
	isValidPlayer(player)
	local char = player.Character or player.CharacterAdded:Wait()
	if char:FindFirstChild("Temperature") then
		return char:FindFirstChild("Temperature").Value
	else
		NewTemperature(player)
		return char:FindFirstChild("Temperature").Value
	end
end

function Temp.SetTemperature(player, setNumber)
	isValidPlayer(player)
	local char = player.Character or player.CharacterAdded:Wait()
	if char:FindFirstChild("Temperature") then
		local temp = char:FindFirstChild("Temperature").Value
		temp = setNumber
		print(temp)
	end
end

return Temp

What I’m expecting is an output print.

15  -  Server - TemperatureRemote:6
10  -  Server - TemperatureModule:32
10  -  Server - TemperatureRemote:10

But real output printing this

15  -  Server - TemperatureRemote:6
10 -  Server - TemperatureModule:32
15 -  Server - TemperatureRemote:10

Any suggestions will be appreciated.

1 Like

It is printing that because you did not set the Value property of the Temperature Instance. You just set the function’s local temp variable to 10.
So it should be:

		char:FindFirstChild("Temperature").Value = setNumber

instead of

		local temp = char:FindFirstChild("Temperature").Value
		temp = setNumber
		print(temp)
2 Likes

As mentioned by @ByteBox, the root of your problem is a misunderstanding between creating a variable pointing to a Value, compared to an Instance. What you are trying to do, is set the Value property of Temperature to the new value (setNumber). However what you are actually doing is creating a variable that is the current Temperature, and then overwriting the variable, rather than actually updating the Value property.

Hopefully this can help you visualize it a bit better:
This is what you are doing

local temp = 15 --char:FindFirstChild("Temperature").Value
temp = 10 --setNumber
print(temp)

This is what you want to do

local temp = char:FindFirstChild("Temperature") -- You are setting the temp variable to be the instance itself, rather than making a copy of the current value
temp.Value = 10 --setNumber
print(temp)

And to add to that, in cases like this where you only reference the instance once, there is really no point in storing it in a variable. So you should end up with code looking like the codeblock @ByteBox provided.

Ah, whoops. Thanks for helping. Now I understand more about local.

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