I want to create a function that runs once when the distance between the player character and the floor is > 50. The problem is that it doesn’t stop after the first run.
All methods i tried:
if runned == true then return end
repeat
until loops = 1
but thats not work.
There is code:
while true do
for i,v in pairs(game.Players:GetChildren()) do
if v:IsA("Player") then
local Char = v.Character or v.CharacterAdded:Wait()
local HRP = Char:WaitForChild("HumanoidRootPart")
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {workspace.Map}
local ray = workspace:Raycast(HRP.Position,Vector3.new(0,-99999,0),params)
if ray then
if ray.Distance < 50 then
--there is rocks spawning
end
end
end
end
wait()
end
How it actually work:
How it must work:
I need to run the function once as soon as the Player character reaches distance < 50, so that it doesn’t run the function again
This loop is always running, unless you have it inside another function.
Instead of print("Done") as your check use print(ray.Distance) so you can find out the actual distance.
The other thing is try this
if ray.Distance < 50
print(ray.Distance)
local ray = workspace:Raycast(HRP.Position,Vector3.new(0,-99999,0),params)
end
Or while ray.Distance < 50 do
for your main loop instead of while true do
U should use “break” to stop any kind of loop, this includes while loops,
Example:
while true do
if runned == true then break end
wait()
end
Additional information:
“return” would also work, butbonly if you did put the statement right at the start of the loop, HOWEVER this would cause the loop to possibly freeze up/ break because the loop will just stop there, if the statement is true and if there is no yielding.
So please use “break” instead of “return” to stop any kind of loop.
Hope this helped
Make a variable that acts as a limiter like what @Blankscarface23 showed you, then use break instead of until since it will stop the whole entire loop.
-- repeat
local LoopLimit = 10
local runs = 1
repeat
-- Main Loop
runs = runs + 1
if runs = LoopLimit then
break
end
-- while true do
local Limit = 5
local runs = 1
while true do
-- Main Loop
runs = runs + 1
if runs == Limit then
break
end
end
This is how I would do the whole stopping loop thingy.