Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to break out of while true loop when getting a remote function fire?

Asked by 4 years ago
Edited 4 years ago

EDIT : Some of my scripts have worked on this, but they were too laggy (0.1 fps) Okay, i was making a game where there's a timer and leaderboard , with a start / end point This is my local script, parent of the timer gui

--a = 0
local RS = game:GetService('RunService')
--RS:BindToRenderStep('',1,function()
    --a = a + 1
    --b = a / 60
    --script.Parent.Text = b


--end)

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(a)
    local starttime = workspace.DistributedGameTime
while true do
    wait()
    local time = workspace.DistributedGameTime - starttime
    script.Parent.Text = time
end
local seconds = script.Parent.Parent.Time

script.Parent.Text = seconds.Value

game.ReplicatedStorage.End.OnClientEvent:Connect(function(a)
    local starttime = workspace.DistributedGameTime
    tim = time
    game.ReplicatedStorage.Submit:FireServer(plr, starttime, workspace.DistributedGameTime, tim)
end
)end)

This is my start brick script, parented to a part


function deathTouch(part) game:GetService("Players") local plr = game.Players:GetPlayerFromCharacter(part.Parent) plr.Character.Humanoid.WalkSpeed = 0 plr.PlayerGui.ScreenGui.TextLabel.Text = "0" wait(0.1) plr.Character:MoveTo(Vector3.new(0,0,0)) wait(0.1) plr.Character:MoveTo(Vector3.new(0,0,0)) wait (0.8) game.ReplicatedStorage.RemoteEvent:FireClient(plr) plr.Character.Humanoid.WalkSpeed = 16 end script.Parent.Touched:connect(deathTouch)

This is my end part, when you touch it the timer ends


function deathTouch(part) game:GetService("Players") local plr = game.Players:GetPlayerFromCharacter(part.Parent) game.ReplicatedStorage.End:FireClient(plr) end script.Parent.Touched:connect(deathTouch)

This is my script in server script service

game.ReplicatedStorage.Submit.OnServerEvent:Connect(function(plr, submittime, endtime, tim)
    print(plr .. submittime .. endtime .. tim)
end)

Remote Events Used : Submit, RemoteEvent, End

Main problem So the problem is the starting brick works, but when I touch the end brick the timer is still stuck in the timer while loop state, i've tried many solutions, any help? (dont ask whats the error there is none) Other problems The timer is slightly inaccurate, i've tried using run service but I am a noob at that and didn't use it Why was this created? A map with a leaderboard and players with the fastest times will be put on leaderboard

0
It´s a loop that detects if something is TRUE. So change that something to false... Sk3pticalR0BL0X 33 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

Very simple. use a break and a boolean value

SERVER SIDE

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

local BreakLoop = false

spawn(function() -- I put a spawn here so that I can continue with code below the while loop its basically a coroutine, but it stays updated. Allows code to jump over a loop while the loop can continue running. 
    while true do
        if BreakLoop == true then
            break
        end
        print'In loop'
        wait(.5)
    end
    print'Loop broken'
end)

print'I can run because of the spawn()'

Event.OnServerEvent:Connect(function(player, ...)
    print'Event fired'
    BreakLoop = true
end)

CLIENT SIDE

wait(4)

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

Event:FireServer()

You should get something like this...

In loop
I can run because of the spawn()
In loop
In loop
In loop
In loop
In loop
In loop
In loop
Event fired
Loop broken
0
I got the basic idea, thanks Jack_Hase 85 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Create a variable which turns to true when whatever is clicked, and make the while true do instead reference if the variable is false instead of looking for true.

0
I tried and the timer isn't counting up now Jack_Hase 85 — 4y

Answer this question