How to make a tempature system for your game

3rd open source of my work so far, hope you enjoy!

I wont be including API page links in this because they are fairly simple concepts

Explorer Setup:

  1. LocalScript in StartCharacterScripts, named whatever you want, for this tutorial ill be naming it Client_TempHandler

  2. Script in ServerScriptService, named whatever you want, I will be naming it “Server_TempatureLoader”

Code:

Now onto the first code, in the Server Script, this is pretty basic to setup, we will detect when the player joins the game and then giving them a number value under their player object so that we can keep a note of their temp anywhere in the game

game.Players.PlayerAdded:Connect(function(player) -- finds the player
	local playerTemp = Instance.new("NumberValue") -- makes the temp value
	playerTemp.Name = "CurrentCharacterTempature" -- sets the name 
	playerTemp.Value = 0 -- set this to whatever your default temp should be, I set it to 0 as my game loads the tempatures depending on what tempature the biome is and what clothing they are wearing
	playerTemp.Parent = player -- sets the values parent to the player object 
end)

Now we need some starting variables, and as always with my work code comments!

local player = game.Players.LocalPlayer -- player object
local character = player.Character or player.CharacterAdded:Wait() -- physical character object
local humanoid = character:WaitForChild("Humanoid") -- humanoid object, ill use this so we can give them effectrs for being too cold/hot

local currentTemp = player:WaitForChild("CurrentCharacterTempature") -- tempature object thats created on the server

local maxTemp = 30 -- max temp value before they get too hot 
local minTemp = 5 -- min temp value before they get too cold

Now onto the functions

local function tooCold()
	humanoid.WalkSpeed = 7 -- if they are too cold it sets their speed down
end

local function tooHot()
	print("Your too hot!") -- I have no punishment for being too hot, come up with something
end

Now we check the value and see if it changes and when it changes we check its value and if it goes past the values we set then we go in the punishments

if currentTemp:IsA("NumberValue") then -- making sure its a number value so we have no confusion
	currentTemp.Changed:Connect(function() -- seeing when the instance changes so that we can detect the values
		
		--detections
		if currentTemp.Value > maxTemp then
			tooHot()
		end

		if currentTemp.Value < minTemp then
			tooCold()
		end

		if currentTemp.Value > minTemp then
			humanoid.WalkSpeed = 16
		end
	end)
else
	error("Hey! Your Server Script didnt load the tempature value correctly, might wanna check that!") -- makes an error if the client doesnt detect the value
end

Here is the entire LocalScript:

local player = game.Players.LocalPlayer -- player object
local character = player.Character or player.CharacterAdded:Wait() -- physical character object
local humanoid = character:WaitForChild("Humanoid") -- humanoid object, ill use this so we can give them effectrs for being too cold/hot

local currentTemp = player:WaitForChild("CurrentCharacterTempature") -- tempature object thats created on the server

local maxTemp = 30 -- max temp value before they get too hot 
local minTemp = 5 -- min temp value before they get too cold

local function tooCold()
	humanoid.WalkSpeed = 7 -- if they are too cold it sets their speed down
end

local function tooHot()
	print("Your too hot!") -- I have no punishment for being too hot, come up with something
end

if currentTemp:IsA("NumberValue") then -- making sure its a number value so we have no confusion
	currentTemp.Changed:Connect(function() -- seeing when the instance changes so that we can detect the values
		
		--detections
		if currentTemp.Value > maxTemp then
			tooHot()
		end

		if currentTemp.Value < minTemp then
			tooCold()
		end

		if currentTemp.Value > minTemp then
			humanoid.WalkSpeed = 16
		end
	end)
else
	error("Hey! Your Server Script didnt load the tempature value correctly, might wanna check that!") -- makes an error if the client doesnt detect the value
end

Example usage:

In my game I use a zone module by ForeverHD for biomes and settings, and that changes it along with clothing updates

local function zoneEnter(player, zonePath)
	local playerTemp = player:FindFirstChild("CurrentCharacterTempature")
	local locationTemp = zonePath:GetAttribute("Tempature")
		
	playerTemp.Value = locationTemp
end

It might be a good idea to handle the punishments on the server instead of the client by detecting on the sever or using RemoteEvents but in lazy

With that I hope everyone can use this and has a good holiday!

Downloadable place:
https://www.roblox.com/games/8366001550/Tempature-Tutorial

Let me know if anyone finds bugs or optimizations!

6 Likes

hey will this work when one part is 100 temperature and when you go further from it,the temperature goes down slowly

2 Likes

It will work but you have to code that yourself using .magnitude checks

3 Likes