What is best method to stay at speed value?

Hello! I’m working on the temperature events like if you touch a hot area, it will increase your temperature repeatedly. Now my issue with speed is that if you touched many zones, it will add more speed. Let me show you, here’s the link for the video Gyazo Video This is a normal speed. Now here’s the issue video Gyazo Video 2

My script in StarterCharacterScripts

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local folder = character:WaitForChild("TemperatureValues")

repeat task.wait() until folder

local temp = folder.Temperature
local HWC = folder.HWC
local speed = folder.Speed

HWC:GetPropertyChangedSignal("Value"):Connect(function()
	if HWC.Value == "Warm" then
		while true do
			if HWC.Value == "Warm" then
				if temp.Value ~= 15 then
					if temp.Value >= 15 then
						temp.Value = temp.Value - 1
					elseif temp.Value <= 15 then
						temp.Value = temp.Value + 1
					end
				end
			else
				break
			end
			task.wait(speed.Value)
		end
	elseif HWC.Value == "Cold" then
		while true do
			if HWC.Value == "Cold" then
				if temp.Value >= 0 then
					temp.Value = temp.Value - 1
					if temp.Value == 0 or temp.Value <= 0 then
						humanoid:TakeDamage(humanoid.MaxHealth)
					end
				end
			else
				break
			end
			task.wait(speed.Value)
		end
	elseif HWC.Value == "Hot" then
		while true do
			if HWC.Value == "Hot" then
				if temp.Value <= 30 then
					temp.Value = temp.Value + 1
					if temp.Value == 30 or temp.Value >= 30 then
						humanoid:TakeDamage(humanoid.MaxHealth)
					end
				end
			else
				break
			end
			task.wait(speed.Value)
		end
	end
end)

I want the speed stay the value

you should be using math.min(), math.max(), or math.clamp() to Handle Minimum and Maximum Values.

You can also use the IntConstrainedValue Instance for this.


Try adding a Debounce to your Character to keep track of the Damages Inputs tho.

Ah, can you give me a little explanation of how I can use math.min and math.max ?

Basically, for math.min() it will look for the smallest Value, for example:

math.min(123,89,345,904) -- 89 will be chosen as it is the smallest

math.max is the opposite, it will look for the largest value:

math.max(123,89,345,904) -- 904 will be chosen as it is the largest

However, for math.clamp is to limit your number to the mininum and maximum integer (or float):

print(math.clamp(213, 0, 100)) -- returns 100 as 213 is over the maximum
print(math.clamp(-13, 0, 100)) -- returns 0 as -13 is less than the minimum

However, this may not be what you are looking for after rereading the post

Why are you yielding the same instance twice?

Let’s say that every time you touch a zone a loop starts where it gives you +15 temperature every second

When you enter a different zone that gives +10 per second, you now have two loops giving you temperature at the same time

When you enter a different zone, you need to disconnect the other loops

Oh. I didn’t think of that. My bad.

i meant speed btw sorry!

thisisjusttoaddmorecharacters

Also you can just have one continuos loop running and it gives you speed based on a variable that you change other places

example

local speedincrement = 15

while true do
player.WalkSpeed += speedincrement
wait(1)
end

where you change the speedincrement each time they enter a different zone

one loop instead 3 loops and it works now.

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local folder = character:WaitForChild("TemperatureValues")

repeat task.wait() until folder

local temp = folder.Temperature
local HWC = folder.HWC
local speed = folder.Speed


local function increase()
	if temp.Value <= 30 then
		temp.Value = temp.Value + 1
		if temp.Value == 30 or temp.Value >= 30 then
			humanoid:TakeDamage(humanoid.MaxHealth)
		end
	end
end

local function decrease()
	if temp.Value >= 0 then
		temp.Value = temp.Value - 1
		if temp.Value == 0 or temp.Value <= 0 then
			humanoid:TakeDamage(humanoid.MaxHealth)
		end
	end
end

local function goalwarmtemp()
	if temp.Value ~= 15 then
		if temp.Value >= 15 then
			temp.Value = temp.Value - 1
		elseif temp.Value <= 15 then
			temp.Value = temp.Value + 1
		end
	end
end

while true do
	if HWC.Value == "Warm" then
		goalwarmtemp()
	elseif HWC.Value == "Cold" then
		decrease()
	elseif HWC.Value == "Hot" then
		increase()
	end
	task.wait(speed.Value)
end

I didn’t even think of this but then I realized I could use one loop. Bruh. My bad

Also

		if temp.Value >= 15 then
			temp.Value = temp.Value - 1
		elseif temp.Value <= 15 then
			temp.Value = temp.Value + 1
		end

doesn’t need the second if.

		if temp.Value >= 15 then
			temp.Value = temp.Value - 1
		else
			temp.Value = temp.Value + 1
		end

Just a technicality, but when you check you shouldn’t use = in both the <= or >= statement since when temp.Value = 15 only the first one fires anyway.

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