Hello, I have a loop and it checks every 0.1 seconds. (part of the game). And I want something to check indefinitely like every frame not 0.1 seconds. However the 0.1 seconds is inside the loop, and the checking is inside the loop aswell. How would I make it inside the loop, but still keep checking every frame and not 0.1 seconds.
Sorry if this was a bit confusing
local player = game.Players.LocalPlayer
local character = player.Character
local Humanoid = player.Character:WaitForChild("Humanoid")
local steps = player.PlayerGui.StepsGUI.Frame.TextLabel
local stagetrans = player.PlayerGui.StageTransfer.CurrentStage
local leaderstats = player.leaderstats.Stage
local mod = script.Parent.StoreSteps
local RunService = game:GetService("RunService")
print(steps.Text)
local data = require(mod)
stagetrans:GetPropertyChangedSignal("Text"):Connect(function()
steps.Text = tostring(data[stagetrans.Text])
end)
while task.wait(.1) do
local new = if tonumber(steps.Text) then steps.Text - 1 else 0
if new <= 15 then
steps.TextColor3 = Color3.new(0.866667, 0, 0.0156863)
steps.Parent.ImageLabel.ImageColor3 = Color3.new(0.866667, 0, 0.0156863)
elseif new > 15 then
steps.TextColor3 = Color3.new(255,255,255)
steps.Parent.ImageLabel.ImageColor3 = Color3.new(255,255,255)
end
if Humanoid.MoveDirection.Magnitude > 0.0005 then
steps.Text = new
end
if new <= 0 then
print('recognized')
Humanoid.Health -=100
game.ReplicatedStorage.TPOnDeath:FireServer(stagetrans.Text)
print("FIRED STEPS EXPIRED")
steps.Text = tostring(data[stagetrans.Text])
break
elseif Humanoid.Health <=0 then --I want to check this part.
game.ReplicatedStorage.TPOnDeath:FireServer(stagetrans.Text)
print("FIRED RESET")
steps.Text = tostring(data[stagetrans.Text])
break
end
end
You can put all of your code inside the function, like so:
local RunService = game:GetService("RunService")
RunService.RenderStepped:Connect(function(dt)
local new = if tonumber(steps.Text) then steps.Text - 1 else 0
if new <= 15 then
steps.TextColor3 = Color3.new(0.866667, 0, 0.0156863)
steps.Parent.ImageLabel.ImageColor3 = Color3.new(0.866667, 0, 0.0156863)
elseif new > 15 then
steps.TextColor3 = Color3.new(255,255,255)
steps.Parent.ImageLabel.ImageColor3 = Color3.new(255,255,255)
end
if Humanoid.MoveDirection.Magnitude > 0.0005 then
steps.Text = new
end
if new <= 0 then
print('recognized')
Humanoid.Health -=100
game.ReplicatedStorage.TPOnDeath:FireServer(stagetrans.Text)
print("FIRED STEPS EXPIRED")
steps.Text = tostring(data[stagetrans.Text])
break
elseif Humanoid.Health <=0 then --I want to check this part.
game.ReplicatedStorage.TPOnDeath:FireServer(stagetrans.Text)
print("FIRED RESET")
steps.Text = tostring(data[stagetrans.Text])
break
end
end)
This works, however it affects everything inside the function. I only want it to affect
elseif Humanoid.Health <=0 then --I want to check this part.
game.ReplicatedStorage.TPOnDeath:FireServer(stagetrans.Text)
print("FIRED RESET")
steps.Text = tostring(data[stagetrans.Text])
break
end
Is a coroutine able to do that? If so what function of it will do that, as I am fairly new to coroutines?
There are many ways to do this, but I think the easiest way is to split the code.
local data = require(mod)
stagetrans:GetPropertyChangedSignal("Text"):Connect(function()
steps.Text = tostring(data[stagetrans.Text])
end)
local new: number
local flag: boolean = true
RunService.RenderStepped:Connect(function(dt)
if new > 0 and Humanoid.Health <=0 then
game.ReplicatedStorage.TPOnDeath:FireServer(stagetrans.Text)
print("FIRED RESET")
steps.Text = tostring(data[stagetrans.Text])
flag = false
end
end)
while flag do
new = if tonumber(steps.Text) then tonumber(steps.Text) - 1 else 0
task.wait(.1)
if new <= 15 then
steps.TextColor3 = Color3.new(0.866667, 0, 0.0156863)
steps.Parent.ImageLabel.ImageColor3 = Color3.new(0.866667, 0, 0.0156863)
elseif new > 15 then
steps.TextColor3 = Color3.new(255,255,255)
steps.Parent.ImageLabel.ImageColor3 = Color3.new(255,255,255)
end
if Humanoid.MoveDirection.Magnitude > 0.0005 then
steps.Text = tostring(new)
end
if new <= 0 then
print('recognized')
Humanoid.Health -=100
game.ReplicatedStorage.TPOnDeath:FireServer(stagetrans.Text)
print("FIRED STEPS EXPIRED")
steps.Text = tostring(data[stagetrans.Text])
break
end
end
You can use a different loop to run the code, so either a while wait or a yield thread.
Here is a snippet of the code with a yield thread: local player = game.Players.LocalPlayer
local character = player.Character
local Humanoid = player.Character:WaitForChild(“Humanoid”)
local steps = player.PlayerGui.StepsGUI.Frame.TextLabel
local stagetrans = player.PlayerGui.StageTransfer.CurrentStage
local leaderstats = player.leaderstats.Stage
local mod = script.Parent.StoreSteps
local RunService = game:GetService(“RunService”)
print(steps.Text)
– Here is the new loop
while true do
local new = if tonumber(steps.Text) then steps.Text - 1 else 0
if new <= 15 then
steps.TextColor3 = Color3.new(0.866667, 0, 0.0156863)
steps.Parent.ImageLabel.ImageColor3 = Color3.new(0.866667, 0, 0.0156863)
elseif new > 15 then
steps.TextColor3 = Color3.new(255,255,255)
steps.Parent.ImageLabel.ImageColor3 = Color3.new(255,255,255)
end
if Humanoid.MoveDirection.Magnitude > 0.0005 then
steps.Text = new
end
if new <= 0 then
print('recognized')
Humanoid.Health -=100
game.ReplicatedStorage.TPOnDeath:FireServer(stagetrans.Text)
print("FIRED STEPS EXPIRED")
steps.Text = tostring(data[stagetrans.Text])
break
elseif Humanoid.Health <=0 then --I want to check this part.
game.ReplicatedStorage.TPOnDeath:FireServer(stagetrans.Text)
print("FIRED RESET")
steps.Text = tostring(data[stagetrans.Text])
break
end
-- Here is the new wait
wait(0.1)