Character Added not detecting character spawning

  1. What do you want to achieve? I want to move player’s character upon spawning to a random selected location.

  2. What is the issue? the CharacterAdded isnt detecting when a character is being spawned into the game, I executed the code without functions in console and it worked.

  3. What solutions have you tried so far? I tried replacing MoveTo() assuming it was causing the issue.

I’m pretty sure It’s a problem caused by delay of default player spawning. I haven’t used studio in over a year so I’ll need some reminding.

local work = game:GetService("Workspace")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local char = player.Character
		local root = char:WaitForChild("HumanoidRootPart", 10)
		local pos = Vector3.new(math.random(0,253), 30, math.random(0,253))
		char:MoveTo(pos)
		print("Spawned "..char.Name.." at "..tostring(pos))
	end)
end)

Just remove this

You’re re-defining your char variable to the singular Player.Character property, and not to the CharacterAdded event

1 Like

I tried something else earlier so I just copy pasted part of a code, thanks, removed it, still didn’t fix it though.

Sometimes the Character can spawn before or after CharacterAdded() runs. This tends to only be a problem in Studio but can happen in the actual game too. You should really just do it like this instead:

local work = game:GetService("Workspace")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local char = player.Character or player.CharacterAdded:Wait()
	local root = char:WaitForChild("HumanoidRootPart", 10)
	local pos = Vector3.new(math.random(0,253), 30, math.random(0,253))
	char:MoveTo(pos)
	print("Spawned "..char.Name.." at "..tostring(pos))
end)

That code will run when the player joins if the character is already in (like in studio), or will wait for the character to appear.

1 Like

Hm, let’s ask these questions then :thinking:

  • What kind of script is this? A Server or Local?
  • Where is this script exactly located?
  • Can you add print statements to thoroughly check where the script stops running?

You could try this and see if it works, but I highly doubt it’ll make any difference

local Players = game:GetService("Players")

local function CharAdded(Char)
    print("Character added:", Char)

    local Root = Char:WaitForChild("HumanoidRootPart")

    local Pos = Vector3.new(math.random(0, 253), 30, math.random(0, 253))
    Char:MoveTo(Pos)

    print("Spawned", Char, "at", Pos)
end

Players.PlayerAdded:Connect(function(Plr)
    print("Player added:", Plr)
    CharAdded(Plr)
end)

Or you could also try CharacterAppearanceLoaded which seems to yield a bit longer as it takes a while for the Character’s avatar to properly load, which could work

1 Like

Server script, located in server script service, it doesnt stop running.

You must use a task.wait() at the start of the the CharacterAdded event as Roblox isn’t instant with setting player locations.

2 Likes

The player.CharacterAdded event only works when clicking “start” on the test bar on roblox studio

1 Like