How would I make this run without disturbing other code?

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 :sweat_smile:

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
1 Like

Hello,

You can use RunService.RenderStepped to have this code run every frame, rather than once every 0.1 seconds.

local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function(dt) --dt for time between frames
    --Code here
end)

There are other functions that run once a frame like Heartbeat and Stepped. More information here: RenderStepped, Stepped, or Heartbeat

1 Like

Can I use this inside the loop? Because all my other stuff inside the loop depends on the 0.1 seconds.

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?

2 Likes

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
1 Like

Don’t use polling if you want to do something when the humanoid dies, use events. So instead of checking every frame, do something like

Humanoid.Died:Connect(function()
    game.Replic.....
    print....
    steps.Text...
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)

local data = require(mod)

stagetrans:GetPropertyChangedSignal(“Text”):Connect(function()
steps.Text = tostring(data[stagetrans.Text])
end)

– Here is the new loop
while true do
local new = if tonumber(steps.Text) then steps.Text - 1 else 0

if new &lt;= 15 then
    steps.TextColor3 = Color3.new(0.866667, 0, 0.0156863)
    steps.Parent.ImageLabel.ImageColor3 = Color3.new(0.866667, 0, 0.0156863)
elseif new &gt; 15 then
    steps.TextColor3 = Color3.new(255,255,255)
    steps.Parent.ImageLabel.ImageColor3 = Color3.new(255,255,255)
end

if Humanoid.MoveDirection.Magnitude &gt; 0.0005 then
    steps.Text = new
end

if new &lt;= 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 &lt;=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)

end