#Any suggestions on how to simplify this

1 messages · Page 1 of 1 (latest)

ripe trail
#
function checkpointService:getCheckpoints(tID, wID)
    local world = worlds:FindFirstChild("WORLD_"..wID)
    if not world then return end
    
    local trails = world:FindFirstChild("trails")
    if not trails then return end
    
    local trail = trails:FindFirstChild("TRAIL_"..tID)
    if not trail then return end
    
    local obby = trail:FindFirstChild("obby")
    if not obby then return end
    
    local checkpoints = obby:FindFirstChild("checkpoints")
    if not checkpoints then return end
    
    return checkpoints:GetChildren()
end
delicate swift
#

Do you not trust that the input values to your function will be sane?

ripe trail
#

i figured, i changed it to this cause i wont exactly know if all trails will contain checkpoints

function checkpointService:getCheckpoints(tID, wID)
    local checkpoints = worlds:FindFirstChild("WORLD_"..wID)
        :FindFirstChild("trails")
        :FindFirstChild("TRAIL_"..tID)
        :FindFirstChild("obby")
        :FindFirstChild("checkpoints")
    
    if not checkpoints then return end
    return checkpoints:GetChildren()
end```
hasty nexus
#

waitforchild

ripe trail
#

server script

hasty nexus
#

but no, you have to check every step

#

could make a function but meh

ripe trail
#

what would happen with an infinite yield would this break the function

hasty nexus
#

probably

ripe trail
#

I might also just get the descandents and check for the checkpoint attribute

#

but I dont want to loop through the whole obby parts

hasty nexus
#
local function FindDescendant(parent:Instance?,d,...)
  return if d then FindDescendant(parent:FindFirstChild(d),...) else parent
end
function checkpointService:getCheckpoints(tID,wID)
  local checkpoints = FindDescendant(worlds,"WORLD_"..wId,"trails","TRAIL_"..tID,"obby","checkpoints")
  return checkpoints and checkpoints:GetChildren()
end```
but like i said that's really meh
ripe trail
#

it always returns nilevilcat

#

I just want it to not break wehn i fuck the data structure up

#

wehn a player would run this function and it gives an error can he use the function again?

hasty nexus
#

i do not recommend using code you don't understand

ripe trail
#

I understand what the code does i think this is a general question wehn a function has an error can you call the function again

hasty nexus
#

ther we go local function FindDescendant(parent:Instance?,d,...) return if d then FindDescendant(parent:FindFirstChild(d),...) else parent end

#

one-liner 😄

ripe trail
#

alr thats too complicated nowevilcat

hasty nexus
#
--!strict
local function FindDescendant(p:Instance?,d,...)
    return if d then FindDescendant(if p then p:FindFirstChild(d) else nil,...) else p
end```
hasty nexus
ripe trail
#

I think I expressed myself incorrectly i didnt wanted to simplify but not fuck everything up wehn I fuck the data tree up in the explorer

#

but if i can call the function even after an error its fine i guess