What do you want to achieve? Keep it simple and clear!
So I am trying to make a horror game with heart rate system.
I want to make it so that when player’s heart rate gone up high, it will slowly decrease until 60
What is the issue? Include screenshots / videos if possible!
It won’t work properly, everytime the heart rate went up it will just decrease very fast and it won’t stop at 60
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Still haven’t got a solution
local HeartRate = script.Parent:WaitForChild("HeartRate")
HeartRate.Changed:Connect(function()
if HeartRate.Value > 60 then
repeat
wait(1)
HeartRate.Value = HeartRate.Value - 1
until HeartRate.Value == 60
end
end)
I’ll have to look more at why it is going down so fast, however before I find out why:
The reason it doesn’t stop at 60 is because it is looking for it to be EXACTLY 60, instead of 60 and below (corrected script below).
local HeartRate = script.Parent:WaitForChild("HeartRate")
HeartRate.Changed:Connect(function()
if HeartRate.Value > 60 then
repeat
wait(1)
HeartRate.Value = HeartRate.Value - 1
until HeartRate.Value <= 60 --If it is equal to, or less than 60, stop
end
end)
edit: yeah, as the person below me pointed out, the .changed thing is causing an issue.
The .Changed is also a problem, everytime the value changes, the HeartRate.Changed function creates another thread and creates another repeat loop essentially, each time it changes it creates +1 repeat loop
local HeartRate = script.Parent:WaitForChild("HeartRate")
local ischanging = false
HeartRate.Changed:Connect(function()
if HeartRate.Value > 60 then
if not ischanging then
ischanging = true
repeat
wait(1)
HeartRate.Value = HeartRate.Value - 1
until HeartRate.Value <= 60
ischanging = false
end
end
end)
It is working fine but I can’t touch the brick that make the heart rate go up again, I can only touch it once. The heart rate won’t go up when I touch it
Here is the brick script if it is the problem
local canhit = false
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local char = hit.Parent
if not canhit then
canhit = true
local value = char:FindFirstChild("HeartRate")
value.Value = 70
wait(3)
canhit = false
end
end
end)
local character = script.Parent
local heartRate = character:WaitForChild("HeartRate")
local isChanging = false
heartRate.Changed:Connect(function()
if isChanging then
return
end
if heartRate.Value > 60 then
isChanging = true
repeat
task.wait(1)
heartRate.Value -= 1
until heartRate.Value <= 60
isChanging = false
end
end)
local part = script.Parent
local cannotHit = false
part.Touched:Connect(function(hit)
if cannotHit then
return
end
if hit.Parent:FindFirstChild("Humanoid") then
cannotHit = true
local character = hit.Parent
if character:FindFirstChild("HeartRate") then
local heartRate = character:FindFirstChild("HeartRate")
heartRate.Value = 60
task.wait(3)
cannotHit = false
end
end
end)
I’m sorry if I did not explain this well enough. The brick is suppose to make the heart rate go up for testing and I when I touch the brick the first time, it went up to 70 and slowy went down to 60 and it work just fine but the problem is when I touch it again it won’t go up and I don’t think it’s the brick’s script that went wrong either or I might be wrong.
Did you try the above with 70 instead? This should go without saying but both need to be server scripts so that the stat change is being correctly replicated over the server.