How do i make my script use first child?

so basicly i made a script that goes thruu spawn points and “should” use the first avaliable, but i cant figure out what line i should use to make it use the first avaliable child instead of looping thruu all the others avaliable.

script:

Folders:
Skærmbillede 2022-10-22 031936

break a line below but before the end where v.Available.Value = false is.

1 Like

If you want to use the first child found in the table you can just select the first one in the table.

local firstSpawn = spawnpoints:GetChildren()[1]
2 Likes

It dosnt work for me. The output just says

ServerScriptService.Script:8: invalid argument #1 to ‘pairs’ (table expected, got Instance)

What is your script now? I may have misread your question and instead you should put a break at the bottom of the v.Available.Value = false line like the first reply suggested.

1 Like

my script now is

local spawnpoints = game.Workspace.SpawnPoints

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
game.ReplicatedStorage.Remotes.FirePlayerSpawn.OnServerEvent:Connect(function()
local firstSpawn = spawnpoints:GetChildren()[1]
for i, v in pairs(firstSpawn) do
if v.Avaliable.Value == true then
local HRP = character:WaitForChild(“HumanoidRootPart”)
HRP.CFrame = CFrame.new(v.Point.Position)
v.Avaliable.Value = false
end
end
end)
end)
end)

I have tried using break but nothing changes for me when using break

Rewrote the script to have clearer names of the variables in the for loop, to not create a giant mess of memory leaks and potentially fixed your problem with the break statement

local spawnpoints = game.Workspace.SpawnPoints

game.ReplicatedStorage.Remotes.FirePlayerSpawn.OnServerEvent:Connect(function(player)
   local character = player.Character or player.CharacterAdded:Wait() --get the character from the player in the remote event

    for _, spawn in spawnpoints:GetChildren() do
        local Available = spawn.Available
        if Available.Value then
            Available.Value = not Available.Value --make it not true, which is false
            local HRP = character:WaitForChild(“HumanoidRootPart”)
            HRP:PivotTo(CFrame.new(spawn.Point.Position))
        
            break --after doing the process on the first spawnpoint, break the loop.
        end
    end
end)
1 Like

IT WORKS (there where also some bugs in my other script but both works now THANKS)

1 Like