Hey, I’m having an issue where this will randomly work and sometimes it’ll randomly not work when I join the game, I have no idea what I’m doing wrong so I’ll provide the script below and the error (another note it works more frequently in studio, if not 100% of the time). Thanks for your time!
Erroring on this line: Character:WaitForChild("HumanoidRootPart").CFrame = respawn2.CFrame + Vector3.new(0,3,0)
Script:
local DataService = game:GetService("DataStoreService")
local DataStore = DataService:GetDataStore("data_01")
local camera = workspace.CurrentCamera
local cpFolder = game.Workspace:WaitForChild("Checkpoints")
game.Players.PlayerAdded:Connect(function(player)
local key = "player-" .. player.UserId
local GetSave = DataStore:GetAsync(key) -- check for existing data
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local hidden = Instance.new("Folder", player)
hidden.Name = "hidden"
local checkpoint = Instance.new("IntValue", leaderstats)
checkpoint.Name = "Stage"
local spawnpoint = Instance.new("IntValue", hidden)
spawnpoint.Name = "SpawnPoint"
if GetSave then
checkpoint.Value = GetSave
print("Data Loaded For " .. player.Name)
else
checkpoint.Value = 1
print("New Data Created For " .. player.Name)
end
local Character = player.Character
local respawn2 = cpFolder:WaitForChild(player.leaderstats.Stage.Value)
Character:WaitForChild("HumanoidRootPart").CFrame = respawn2.CFrame + Vector3.new(0,3,0)
player.CharacterAdded:Connect(function(character)
repeat wait() until workspace:FindFirstChild(character.Name)
local player = game.Players:GetPlayerFromCharacter(character)
local respawn = cpFolder[player.hidden.SpawnPoint.Value]
character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,3,0)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = "player-" .. player.UserId
DataStore:SetAsync(key, player.leaderstats.Stage.Value)
print("Data Successfully Saved For " .. player.Name)
end)
What do you mean? What exactly doesn’t work, does is still throw the same error?
You’re defining the character as player.Character but the character likely hasn’t loaded in since you’re doing it on the server. The CharacterAdded event would fire once the character has fully loaded in, if it doesn’t exist prior.
So I would replace: local Character = player.Character with local character = player.Character or player.CharacterAdded:Wait()
Moreover, you’re getting the error before the CharacterAdded event so it would be a problem defining your character.
I just tested it again to make sure and I’m still getting the error.
game.Players.PlayerAdded:Connect(function(player)
local key = "player-" .. player.UserId
local GetSave = DataStore:GetAsync(key) -- check for existing data
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local hidden = Instance.new("Folder", player)
hidden.Name = "hidden"
local checkpoint = Instance.new("IntValue", leaderstats)
checkpoint.Name = "Stage"
local spawnpoint = Instance.new("IntValue", hidden)
spawnpoint.Name = "SpawnPoint"
if GetSave then
checkpoint.Value = GetSave
print("Data Loaded For " .. player.Name)
else
checkpoint.Value = 1
print("New Data Created For " .. player.Name)
end
local character = player.Character
local respawn2 = cpFolder:WaitForChild(player.leaderstats.Stage.Value)
character:WaitForChild("HumanoidRootPart").CFrame = respawn2.CFrame + Vector3.new(0,3,0)
player.CharacterAdded:Connect(function(character)
repeat wait() until workspace:FindFirstChild(character.Name)
local player = game.Players:GetPlayerFromCharacter(character)
local respawn = cpFolder[player.hidden.SpawnPoint.Value]
character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,3,0)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
game.Players.PlayerAdded:Connect(function(player)
local key = "player-" .. player.UserId
local GetSave = DataStore:GetAsync(key) -- check for existing data
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local hidden = Instance.new("Folder", player)
hidden.Name = "hidden"
local checkpoint = Instance.new("IntValue", leaderstats)
checkpoint.Name = "Stage"
local spawnpoint = Instance.new("IntValue", hidden)
spawnpoint.Name = "SpawnPoint"
if GetSave then
checkpoint.Value = GetSave
print("Data Loaded For " .. player.Name)
else
checkpoint.Value = 1
print("New Data Created For " .. player.Name)
end
local character = player.Character or player.CharacterAdded:Wait()
local respawn2 = cpFolder:WaitForChild(player.leaderstats.Stage.Value)
character:WaitForChild("HumanoidRootPart").CFrame = respawn2.CFrame + Vector3.new(0,3,0)
player.CharacterAdded:Connect(function(char)
local respawn = cpFolder[player.hidden.SpawnPoint.Value]
char:WaitForChild("HumanoidRootPart").CFrame = respawn.CFrame + Vector3.new(0,3,0)
end)
end)