Need help with waitforchild

greetings all, this is a script in which i have in serverscriptservice, it’s meant to be a spawnpoint for players on a certain team and with certain rank, it works perfectly fine when i test in studio, but in the actual game it doesn’t work because i believe the part hasn’t loaded in, i attempted to put a wait for child in, but it didn’t seem to help, any solutions?


local SpawnPart = game.Workspace.cityadmin -- Set this to the part you want (Not a spawn part but a regular part)
local GroupID = 16221838 --Set this to your group id
workspace:WaitForChild("cityadmin")
game.Players.PlayerAdded:Connect(function(player)
	if player:GetRankInGroup(GroupID) == 254 and player.Team.Name == "Combine" then
		player.Character:MoveTo(SpawnPart.Position+Vector3.new(0,2.5,0))
		player.CharacterAdded:Connect(function(char)
			wait()
			char:MoveTo(SpawnPart.Position+Vector3.new(0,2.5,0))
		end)
	end
end)

1 Like

The wait for child doesn’t seem to be defined in a variable, can anyone confirm this?

You used it incorrectly. WaitForChild yields until the given instance exists and returns it (with an optional timeout). SpawnPart will be nil due to the fact that it has not loaded in when you reference it, so Luau returns nil. You should do this:

--use capital letter variables for objects and classes
local spawnPart = workspace:WaitForChild("cityadmin")

This should work

local cityadmin = workspace:WaitForChild("cityadmin")
local GroupID = 16221838

game.Players.PlayerAdded:Connect(function(player)
    local function onCharacterAdded(char)
        char:WaitForChild("HumanoidRootPart")
        char:MoveTo(cityadmin.Position + Vector3.new(0, 2.5, 0))
    end
    if player:GetRankInGroup(GroupID) == 254 and player.Team and player.Team.Name == "Combine" then player.CharacterAdded:Connect(onCharacterAdded)
        if player.Character then
        end
    end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.