How do I fix this spawn script?

Any help would be greatly appreciated, currently it doesn’t teleport to the spawn point for unknown reasons

local spawnpoint = game.Workspace.SpawnTest

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		player.Character.HumanoidRootPart.Position = spawnpoint.Position
	end)
end)

No no no!!! Please use Model:SetPrimaryPartCFrame()

Something like this…

local spawnpoint = game.Workspace.SpawnTest

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		player.Character:SetPrimaryPartCFrame(spawnpoint.CFrame)
	end)
end)

Have you also checked if this is a server script? It should be a server script.

I appreciate the effort but this didn’t fix the issue

Have you also checked if this is a server script? It should be a server script.

You should use :PivotTo() instead of :SetPrimaryPartCFrame()

It is…


Okay, I don’t think the issue here is the code- rather an Environmental issue. Have you looked through other scripts, checked for silly little toggles, (Disabled = true), etc.

Did you try running it within your own game because it’s not working for me even in baseplates with just the spawnpoint and script

Nope, I will try running it now!

After testing this code and a few proposed solutions myself, I discovered a solution that works (even without using SetPrimaryPartCFrame)

If you add task.wait() before teleporting the player to the spawn, it will work.

Here is how you could insert it into your code (I also added a couple of optimizations to your current code)

local Players = game:GetService("Players")
local SpawnPoint = workspace:WaitForChild("SpawnTest")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		task.wait()
		char.HumanoidRootPart.CFrame = SpawnPoint.CFrame
	end)
end)

And an alternative solution using SetPrimaryPartCFrame:

local Players = game:GetService("Players")
local SpawnPoint = workspace:WaitForChild("SpawnTest")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		task.wait()
		char:SetPrimaryPartCFrame(SpawnPoint.CFrame)
	end)
end)
2 Likes

I will try these rn, thanks for your help

If you are teleporting the player. Try this:

local Players = game:GetService("Players")

local SpawnPoint = workspace.SpawnTest

Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Hit)
      local Player = Players:GetPlayerFromCharacter(Hit.Parent)
      
      if Player then
        Player.Character.HumanoidRootPart.CFrame:PivotTo(SpawnPoint.CFrame)
      end
   end)
end)

You should always use :PivotTo instead of :SetPrimaryPartCFrame()

1 Like

This fixed my problem, thanks again for your help :smiley:

1 Like

Hello! I’ve tried testing this, and I’ve realised the issue is- waiting. I added a task.wait(2) between the bricks- it seems to have fixed it. :grin:

player.CharacterAdded:Connect(function()
    task.wait(2)
	player.Character:SetPrimaryPartCFrame(spawnpoint.CFrame)
end)

FYI, try to stay away from forcefully yielding code since Event-Based scripting is much more performant and less hacky

The reason you can’t pivot the character immediately (I believe) is because it’s WorldPivot is still property-locked upon initiation. Thankfully we can use task.defer to schedule PivotTo later in the frame

game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Char)
		task.defer(function()
			Char:PivotTo(SpawnPoint.CFrame)
		end)
	end)
end)

Oh, also on a less-related note for some odd reason Players aren’t destroyed properly meaning their connections aren’t disconnected correctly. Just put this in a server script to prevent a memory leak

game:GetService("Players").PlayerRemoving:Connect(function(Player)
	task.defer(game.Destroy,Player)
end)