Players aren't spawning in correct SpawnLocation

I’m using a ModuleScript named “PlayerManager” to spawn players to the map once the game has started. The problem occurs when attempting to relocate the player via CFrame, Position, or SetPrimaryPartCFrame().

It works for the most part when I test the game alone, however when I play with others I receive an error that the CFrame is nil. This 1 error causes the Game Loop to stop. It’s odd because I’m also checking to see if the Character even exists. As a workaround I’m able to set the player’s location with “player.RespawnLocation = whichSpawn” however the Player either spawns above the SpawnLocation (I changed Player CollisionGroup so players won’t spawn in Lobby which is directly above Map) or around it.

SpawnLocation sizes are 12,1,12 with no collisions and are anchored. I included all the lines I attempted already which can be determined by the Lua comments.

Here’s my code for the ModuleScript function that’s causing the issue, I used Roblox’s Battle Royale template to guide the setup of this code structure.

local function preparePlayer(player, whichSpawn)
    camOnEvent:FireAllClients()
	
    repeat wait() until player.Character -- wait for new character so no error 
    local character = player.Character or player.CharacterAdded:wait()

    if character and player then
	    for i,v in pairs(character:GetDescendants()) do
		    if (v:IsA("BasePart")) then
			    PhysicsService:SetPartCollisionGroup(v, playerGroup)
		    end
	    end
	
	    wait()
	    character:WaitForChild("HumanoidRootPart").CFrame = whichSpawn.CFrame + Vector3.new(0,10,0)
	    --character.HumanoidRootPart.Position = whichSpawn.Position + Vector3.new(0,10,0)
	    --character:SetPrimaryPartCFrame(whichSpawn.CFrame * CFrame.new(0, 10, 0))
	    --player.RespawnLocation = whichSpawn
	
	    player:WaitForChild("PlayerGui").BackpackHotbar.Enabled = true
	    player:WaitForChild("PlayerGui").BackpackHotbar.HotbarLogic.Disabled = false

	    toolHandler(player, character) -- Because players will own different Bombs, each new bomb is saved after purchase

	    local humanoid = character:WaitForChild("Humanoid")

	    humanoid.Died:Connect(function()
		    player:WaitForChild("PlayerGui").BackpackHotbar.Enabled = false
		    player:WaitForChild("PlayerGui").BackpackHotbar.HotbarLogic.Disabled = true

		    matchesGiver.giveMatch(player) -- Give Match/Round to Player
		    removeActivePlayer(player)

		    wait(5) -- Delay a little before respawning player

		    respawnPlayerInLobby(player)
	    end)
    else
        character = player.CharacterAdded:wait()
    end
end

Thanks for the guidance, fairly new to creating games via Roblox yet experience in other engines. My mistake if this has been submitted in wrong forum thread.

1 Like

If I was to make a best guess without debugging it, I’m assuming you’re receiving an error such as: “CFrame is not a valid member of nil”.

I believe the problem lies with your ‘whichSpawn’ parameter being null when you assume it’s a value. I recommend using a breakpoint at the start of the function, and using the watch to observe the provided parameters to be sure.

Alternatively you can also just use some debug prints and print out the variables you want to check. :grin:

If the problem does lie with this parameter, then you simply need to work backwards and ensure the code that is calling this function is passing the parameters you want properly.

Best of luck, I look forward to hearing back.

1 Like

Hmm so I did print(whichSpawn.Name) with what you referred and it couldn’t find the Name of the spawn which shows that it wasn’t loaded/detected yet by the function. This is weird as I don’t get this issue testing locally. The initiating error was “CFrame is not a valid member of nil" though. I’m going to try to wait for the spawn and see if it still occurs, if not I will close the thread as Solved with Solution.

Thanks @Azatiel