Hey! Im trying to make my Player with checkpoint data spawn AT their checkpoint using a player.CharacterAdded event. Although, it only will run AFTER the user joins. Meaning, when they first join the game, my script won’t run. Is this normal for a character added event? I’ve tried adding waits to see if the code is running too fast but that doesn’t seem to work. If someone can find a way to make the code run right when the player joins the game, please let me know!
player.CharacterAdded:Connect(function(character)
local Humanoid = character:WaitForChild("Humanoid")
local Torso = character:WaitForChild("HumanoidRootPart")
wait(0.5)
if Torso and Humanoid then
if player.leaderstats.Stage.Value ~=0 then
local StagePart = workspace.Stages:FindFirstChild(Stage.Value)
Torso.CFrame = StagePart.CFrame + Vector3.new(0,1,0)
end
end
end)
The most probable issue is that the client somehow went before the server? I don’t know if that’s the truth. Make sure that Character.Parent exists, because sometimes it hasn’t been parented into the game.
If that doesn’t work, you might have yielded something before the event was even connected. Otherwise, could you provide the entire code before the function?
I think a way you could is wait for the character to exist the first time, then run the code you want to run and then set up the characteradded evnet?
game.Players.PlayerAdded:Connect(function(player)
local function setPosition(char)
local Humanoid = char:WaitForChild("Humanoid")
local Torso = char:WaitForChild("HumanoidRootPart")
wait(0.5)
if Torso and Humanoid then
if player.leaderstats.Stage.Value ~= 0 then
local StagePart = workspace.Stages:FindFirstChild(Stage.Value)
Torso.CFrame = StagePart.CFrame + Vector3.new(0,1,0)
end
end
end
player.CharacterAdded:Wait()
setPosition(player.Character)
player.CharacterAdded:Connect(setPosition)
end)
player.CharacterAdded:Connect(function(character)
local Humanoid = character:WaitForChild("Humanoid")
local Torso = character:WaitForChild("HumanoidRootPart")
wait(0.5)
if Torso and Humanoid then
if player.leaderstats.Stage.Value ~=0 then
local StagePart = workspace.Stages:FindFirstChild(player.leaderstats.Stage.Value)
Torso.CFrame = StagePart.CFrame + Vector3.new(0,1,0)
end
end
end)
local Stats = Instance.new("Folder")
Stats.Name = "leaderstats"
Stats.Parent = player
local Stage = Instance.new("NumberValue")
Stage.Name = "Stage"
Stage.Parent = Stats
Stage.Value = 0
local Data = SaveDataStore:GetAsync(player.UserId)
if Data then
print(Data.Stage)
for i,stats in pairs(Stats:GetChildren()) do
stats.Value = Data[stats.Name]
end
else
print(player.Name .. " has no data.")
end
player.CharacterAdded:Connect(function(character)
local Humanoid = character:WaitForChild("Humanoid")
local Torso = character:WaitForChild("HumanoidRootPart")
wait(0.5)
if Torso and Humanoid then
if player.leaderstats.Stage.Value ~=0 then
local StagePart = workspace.Stages:FindFirstChild(Stage.Value)
Torso.CFrame = StagePart.CFrame + Vector3.new(0,1,0)
end
end
end)