What do you want to achieve? I want to move player’s character upon spawning to a random selected location.
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.
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)
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.
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