Freezing script not working

Hello!
I am having troubles with getting my script to work. It is supposed to make lose tempreature every 2 minutes, and once you get to a tempreature of 93, you start to freeze to death. Once it gets to 93 you lose 25 percent of your max health every time it decreases. The problem is that it does not decrease the Health at all. Here is my code.

local temp = 98
local counter = 93
local player = game.Players.LocalPlayer
local character = script.Parent
local humanoid = character:FindFirstChild("Humanoid")
local health = humanoid.Health
local sound = player.PlayerGui:FindFirstChild("Freezing")
local GUIText = game.ReplicatedStorage:WaitForChild("GUIText")
while true do
	GUIText.Value = temp
	wait(120)
	temp = temp-1
	if temp == counter then
		health = health - (humanoid.MaxHealth/4)
		counter = counter-1
end
	end
2 Likes

is this a localscript?

if it is, well itā€™s not gonna work

it is. :frowning: is there anyway to make it work as a local script

This should work if placed correctly, but remember that exploiters can also change their health as well. Try placing the script in StarterCharacterScripts if you havenā€™t already.

1 Like

yes, you can make a remotevent in replicatedstorage, call it Spawn

then make a script in serverscriptservice

paste this code in:

oh nvm im on mobile, rip cnaā€™t help you sorry.

You are checking if temp is equal to counter, like this:

if temp == counter then

Instead you need to check if temp is equal or lower:

if temp <= counter then

Also, instead of ā€œtemp = temp-1ā€ you can do this:

temp -= 1

change your variable for character.

get the player with the playerAdded event,
i.e. game.Players.playerAdded:Connect(function(plr)

then, use the plr parameter that you just made, and make your var plr.Character

I already have it in startercharacterscripts

I put it temp == counter because I only want it to decrease 4 times because it is 25 percent of their max health. after it does that function I make counter = counter -1. If I put it temp<= counter, wouldnā€™t it just constantly decrease their health?

I think I have found the problem. You created a value called health based on humanoid.Health. That health value is just a number that represents the health the player had when you have set it, not the actual player health. Instead, you have to make humanoid.Health = humanoid.Health-(humanoid.MaxHealth/4) or humanoid.Health-=(humanoid.MaxHealth/4)

1 Like

Hey so that worked, but now I have another issue. I am trying to also make it so you get health when you are around a campfire, which I called heatArea. I made the code but the problem is now you donā€™t lose health or gain health at all.

local temp = 98.6
local counter = 93

local player = game.Players.LocalPlayer
local character = script.Parent
local humanoid = character:FindFirstChild("Humanoid")
local heatArea = game.Workspace.Campfire.Heat
local sound = player.PlayerGui:FindFirstChild("Freezing")


local GUIText = game.ReplicatedStorage:WaitForChild("GUIText")


while true do
	wait()
	GUIText.Value = temp
	if heatArea.Touched == true then
		wait(3)
		temp = temp + 0.5
		counter = counter + 0.5
	elseif heatArea.Touched == false then
		wait(5)
		temp = temp - 0.5
		if temp <= counter then
			humanoid.Health = humanoid.Health - (humanoid.MaxHealth/4)
			counter = counter-1
			sound:Play()
		end
	end
end

Touched is an event, not a condition, so you cant use it to check if a part is touching something else. Instead you can use GetTouchingParts() to get an array of parts that are touching the part you selected:

local parts = HumanoidRootPart:GetTouchingParts()
for i=1,#parts do
if parts[i].Name == ā€œHeatā€ and parts[i].Parent.Name == ā€œCampfireā€ then
ā€“put heat effect here
end
end

I am pretty confused on how that works, I donā€™t get how to put that into my script

HumanoidRootPart is an invisible part inside your character that is located around the torso. the local parts = HumanoidRootParts:GetTouchingParts() will return an array with parts that collided with your character. An array is a class that can hold many values, here is an example:

local examplearray = {value1,value2,value3,ā€¦}

If you use # before the name of an array you set, you get the number of elements on that array. In this case, the number of parts collided. Also, you can find a specific value by using its number. For example:

examplearray = {value1,value2,value3}
ā€“if you type examplearray[1] it will return value1, if you type examplearray[2] it returns value2, and so on.

With this in mind, I can make an iteration of all parts that touched the player, applying the same type of code to every one of them:

for i=1,#parts do

The line of code mentioned starts an iteration that will be repeated by the same amount of parts found by GetTouchingParts(), and the ā€˜iā€™ value will be different on each iteration, starting from 1 until it reaches #parts. You donā€™t need to name it ā€˜iā€™ specifically, but I usually call it that.

if parts[i].Name == ā€œHeatā€ and parts[i].Parent.Name == ā€œCampfireā€ then

On the previous line the ā€˜iā€™ in parts[i] will make so in every iteration a different ā€˜partsā€™ value will be called, starting from parts[1], then parts[2], until it reaches parts[#parts], the last value. This will let you apply the same code you want to all of the values, which is in this case to check if they are named ā€œHeatā€ and if its parentā€™s name is ā€œCampfireā€

ā€“put heat effect code here
endā€“to end the if condition
end-- to end the ā€˜for iā€™ iteration

1 Like