I want it so when the player is not touching the brick anymore, it stops removing the value at line 9
function onTouched(hit) hit = game:GetService("Players").LocalPlayer shower = true while shower == true do wait(1) if hit ~= nil then if hit.PlayerGui.UI.Sections.Fatigue.Fatigue.Value >= 5 then hit.PlayerGui.UI.Sections.Fatigue.Fatigue.Value = hit.PlayerGui.UI.Sections.Fatigue.Fatigue.Value-5 end elseif hit == nil then shower = false end end end script.Parent.Touched:connect(onTouched)
To do what you want to do, you need to use the TouchEnded
event.
The TouchEnded Event is similiar to the Touched
Event, but rather than the initiation of the touch.. it's when you stop touching it(:
Also, on line 2
, you're using LocalPlayer to get the player. This won't work as expected because the code should be a Server-script, and LocalPlayer only works in LocalScripts.
So, an alternative is to use the GetPlayerFromCharacter
function. Use hit.Parent
for the arguments as that is the potential character.
script.Parent.Touched:connect(function(hit) local plr = game.Players:GetPlayerFromCharacter(hit.Parent) local shower = true if plr then local val = plr.PlayerGui.UI.Sections.Fatigue.Fatigue --Make a thread so that code beyond the while loop can run(: coroutine.wrap(function() while shower do wait(1) if val.Value >= 5 then val.Value = val.Value - 5 end end end)() end script.Parent.TouchEnded:connect(function() shower = false end) end)