Need Help With Constantly Lowering a intVal When Touching A Part

Ok so I have a player temperature intVal in StarterCharacterScripts thats Value is set at 100, and I’d like it to go down depending on which zone the player is inside “Zone”(Part) as long as the player is touching or inside the part, the intVal will go down, once the player leaves that zone that number will rise. Heres what i have but it’s not working correctly.

local low = game.Workspace.LowCold
local med = game.Workspace.MedCold
local high = game.Workspace.HighCold

local plr = game.Players.LocalPlayer

local frostBite = 10
local interval = 1/30
local debounce = false
local touchingHumanoids = {}

low.Touched:Connect(function(hit)
	debounce = true
	
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		touchingHumanoids[humanoid] = true
	end
end)

low.TouchEnded:Connect(function(hit)
	debounce = false
	
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		touchingHumanoids[humanoid] = nil
	end
end)


while debounce == true do
	for humanoid in pairs(touchingHumanoids) do
		plr.Character.TempVal.Value = plr.Character.TempVal.Value - frostBite
		
	end
	wait(interval)
end

while debounce == false do
	for humanoid in pairs(touchingHumanoids) do
		plr.Character.TempVal.Value = plr.Character.TempVal.Value + frostBite
	end
	wait(interval)
end

For your zone, I recommend using Region3 or GetPartsBoundInBox.

Ok I tried this but for some odd reason, instead of it just taking 1 away every second, it takes more than 1 at a time, taking it down very quickly.

local lowCold = game.Workspace.LowCold

local region = Region3.new(lowCold.Position - lowCold.Size/2, lowCold.Position + lowCold.Size/2)

local plr = game.Players.LocalPlayer

while true do
	wait(1)
	if plr.Parent:FindFirstChild("Humanoid") then
		local human = plr.Parent
		human.TempVal.Value = human.TempVal.Value - 1
	end
end

robloxapp-20240114-0033418.wmv (2.1 MB)

For the wait(), you should be using task.wait() as wait() is outdated and also does not update as fast.

This is just a suggestion, but you can use -=/+= when adding/subtracting from values.
Example:

human.TempVal.Value -= 1

I made those small changes and it’s still taken way too much away from the Value to quickly, I’m so confused.

Another suggestion: Use :WaitForChild() when getting Instances(the LowCold part) as the script will fail if the Instance does not load fast enough.

Try this, just tested it and it works:

local lowCold = game.Workspace:WaitForChild("LowCold")

local plr = game.Players.LocalPlayer

local debounce = false

while wait(0.1) do
	local touchingParts = game.Workspace:GetPartsInPart(lowCold)

	for _, part in touchingParts do
		if part.Parent then
			if part.Parent:FindFirstChild("Humanoid") then
				if part.Parent.Name == plr.Name then
					if debounce == false then
						debounce = true
						
						part.Parent.TempVal.Value -= 1
						wait(1)
						debounce = false
					end
				end
			end
		end
	end
end

1 Like

thank you I’ll still have some small tweak’s, like adding the TempVal to go up when not in the LowCold area but it seems to be working A LOT better, thx!

P.S. the only thing i notice is it still seems to go down for a little bit of time before completely stopping, even after I leave the zone, would that have anything to do with the wait time?

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