I’m trying to make a closet hiding system that kills you if you hide for too long
the while loop doesn’t stop if I exit the closet
here is my script
local replicatedstorage = game:GetService("ReplicatedStorage")
local changecamevent = replicatedstorage.Events:WaitForChild("changecamera")
local resetcamevent = replicatedstorage.Events:WaitForChild("resetcamera")
local DisableControlsevent = replicatedstorage.Events:WaitForChild("DisableControls")
local enablecontrolsevent = replicatedstorage.Events:WaitForChild("enablecontrols")
local userinputservice = game:GetService("UserInputService")
local plr = game:GetService("Players").PlayerAdded:Wait()
local timeincloset = 0
local colorcorrection = game:GetService("Lighting"):WaitForChild("ColorCorrection")
local debounce = false
local cd = 2.5
local promt = script.Parent.ProximityPrompt
local door1 = script.Parent.Parent.door1
local door2 = script.Parent.Parent.door2
local wtp = script.Parent.Parent.wtp
local wtp2 = script.Parent.Parent.wtp2
local cam1 = script.Parent.Parent.cam1
local cam2 = script.Parent.Parent.cam2
local opensound = script["Closet Open"]
opensound.Volume = 2
local closesound = script["Close Closet Door"]
closesound.Volume = 1
local hiding = plr:WaitForChild("Hiding")
hiding.Value = false
promt.Triggered:Connect(function()
if hiding.Value == false then
if debounce == false then
opensound:Play()
DisableControlsevent:FireClient(plr)
debounce = true
promt.Enabled = false
hiding.Value = true
door1.CanCollide = false
door2.CanCollide = false
changecamevent:FireClient(plr, cam1)
wait(0.5)
plr.Character.Humanoid:MoveTo(wtp.Position)
wait(0.5)
plr.Character.Humanoid.WalkSpeed = 0
for i, v in pairs(plr.Character:GetDescendants()) do
if v:IsA("BasePart") or v:IsA("Decal") then
v.Transparency = 1
end
end
changecamevent:FireClient(plr, cam2)
door1.CanCollide = true
door2.CanCollide = true
closesound:Play()
promt.ActionText = "Exit"
promt.Enabled = true
while true do
wait()
while timeincloset < 5 do
wait(1)
timeincloset += 1
colorcorrection.Saturation -= 0.15
end
while timeincloset == 4 do
wait(1)
timeincloset += 1
end
if timeincloset == 5 then
colorcorrection.Saturation = 0
colorcorrection.TintColor = Color3.new(1, 0, 0)
end
if colorcorrection.TintColor == Color3.new(1, 0, 0) then
timeincloset = 0
hiding.Value = false
debounce = false
wait(1)
plr.Character.Humanoid.Health = 0
colorcorrection.TintColor = Color3.new(1, 1, 1)
resetcamevent:FireClient(plr)
promt.ActionText = "Hide"
promt.Enabled = true
enablecontrolsevent:FireClient(plr)
break
end
end
end
end
end)
promt.Triggered:Connect(function()
if hiding.Value == true then
timeincloset = 0
opensound:Play()
hiding.Value = false
promt.Enabled = false
door1.CanCollide = false
door2.CanCollide = false
plr.Character.Humanoid.WalkSpeed = 16
wait(0.1)
for i, v in pairs(plr.Character:GetDescendants()) do
if v:IsA("BasePart") or v:IsA("Decal") then
v.Transparency = 0
end
if v.Name == "HumanoidRootPart" then
v.Transparency = 1
end
end
plr.Character.Humanoid:MoveTo(wtp2.Position)
resetcamevent:FireClient(plr)
wait(0.5)
closesound:Play()
door1.CanCollide = true
door2.CanCollide = true
enablecontrolsevent:FireClient(plr)
wait(cd)
debounce = false
promt.ActionText = "Hide"
promt.Enabled = true
end
end)
this is the part of the script i need help with
while true do
wait()
while timeincloset < 5 do
wait(1)
timeincloset += 1
colorcorrection.Saturation -= 0.15
end
while timeincloset == 4 do
wait(1)
timeincloset += 1
end
if timeincloset == 5 then
colorcorrection.Saturation = 0
colorcorrection.TintColor = Color3.new(1, 0, 0)
end
if colorcorrection.TintColor == Color3.new(1, 0, 0) then
timeincloset = 0
hiding.Value = false
debounce = false
wait(1)
plr.Character.Humanoid.Health = 0
colorcorrection.TintColor = Color3.new(1, 1, 1)
resetcamevent:FireClient(plr)
promt.ActionText = "Hide"
promt.Enabled = true
enablecontrolsevent:FireClient(plr)
break
end
end
I’ve tried moving the while loop but it just doesn’t work at all unless its where i put it