The code is in ServerScriptService and runs perfectly fine with 0 errors. It prints and fires perfectly but for some reason it wont move the character.
This wouldn’t be a problem with loading either as :MoveTo() works without a Primary Part if I’m correct. Could this just be me glossing over something really obvious?
It fires correctly upon using the event, but not when the player is added.
local respawn_event = game:GetService("ReplicatedStorage"):WaitForChild("RespawnCharacter")
function respawnchr(chr, state)
if state == "game" then
print("was game")
local gamespawn = math.random(1, #workspace:WaitForChild("GameArea"):WaitForChild("Spawns"):GetChildren())
local spawnlocation = workspace:WaitForChild("GameArea"):WaitForChild("Spawns"):GetChildren()[gamespawn]
print(spawnlocation.Position)
chr:MoveTo(spawnlocation.Position)
print("sent")
else
print("was lobby")
local lobbyspawn = math.random(1, #workspace:WaitForChild("SpawnArea"):WaitForChild("Spawns"):GetChildren())
local spawnlocation = workspace:WaitForChild("SpawnArea"):WaitForChild("Spawns"):GetChildren()[lobbyspawn]
print(spawnlocation.Position)
chr:MoveTo(spawnlocation.Position)
print("sent")
end
end
local function CharacterAdded(char)
respawnchr(char)
end
local function PlayerAdded(player)
player.CharacterAdded:Connect(CharacterAdded)
local char = player.Character
if char then
CharacterAdded(char)
end
end
for i,v in pairs(game.Players:GetPlayers()) do
PlayerAdded(v)
end
respawn_event.OnServerEvent:Connect(function(plr, state)
local char = plr.Character or plr.CharacterAdded:Wait()
respawnchr(char, state)
end)
game.Players.PlayerAdded:Connect(PlayerAdded)
Was just having this problem, and here was the only thing I could come up with that actually worked for me.
local character = player.Character
local humanoid
while not character or not humanoid do
character = player.Character
humanoid = character and character:FindFirstChild("Humanoid")
print("Not yet")
wait()
end
Modify this for your code
would probably also check if the player leaves during that loop
this loop has to be inside the respawn i have tryed this way but if the character died you will get the same issue here’s my solution which’s not the best but it get the job done:
function respawnchr(char: Model, state)
if state == "game" then
print("was game")
local gamespawn = math.random(1, #workspace:WaitForChild("GameArea"):WaitForChild("Spawns"):GetChildren())
local spawnlocation = workspace:WaitForChild("GameArea"):WaitForChild("Spawns"):GetChildren()[gamespawn]
print(spawnlocation.Position)
char:MoveTo(spawnlocation.Position)
print("sent")
else
print("was lobby")
local lobbyspawn = math.random(1, #workspace:WaitForChild("SpawnArea"):WaitForChild("Spawns"):GetChildren())
local spawnlocation = workspace:WaitForChild("SpawnArea"):WaitForChild("Spawns"):GetChildren()[lobbyspawn]
char:PivotTo((spawnlocation.CFrame))
task.spawn(function()
while char:GetPivot() ~= spawnlocation.CFrame do
char:PivotTo(spawnlocation.CFrame)
task.wait()
end
print("sent")
end)
end
end
The delay is necessary to override the default roblox spawn system due to thr character loading order as described on the documentation.
It is also given as the code sample for disabling autoload.
Character Loading Event order
Calling the Player:LoadCharacter() with an R15 Avatar fires events in the following order (Note: R6 ordering is different):
Player.Character sets
Player.CharacterAdded fires
Player.Changed fires with a value of “Character”
Character appearance initializes
Player.CharacterAppearanceLoaded fires
Character.Parent sets to the DataModel
The Character rig builds, and the Character scales
Character moves to the spawn location