I am making an obby checkpoint system with saves and am running into the following issue
Stage is not a valid member of Folder
here are my code snippets: script in checkpoint part
if humanoid then print(plr.Parent.Name) local player1 = game:GetService("Players"):GetPlayerFromCharacter(workspace[plr.Parent.Name]) print(type(player1)) local spawns = datastoreservice:GetDataStore("spawns") print(player1.userId) spawns:SetAsync(player1.userId, encodeCFrame(player1.Character:WaitForChild("HumanoidRootPart").CFrame)) -- printTable(player1.leaderstats:GetChildren()) game:GetService("Players")[player1.Name].leaderstats["Stage"] = script.Parent.Name end end)
script in serverscriptservice
game:GetService('Players').PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local level = Instance.new("IntValue", leaderstats) level.Name = "Stage" level.Value = 1 print(player.leaderstats.Stage)
Please could someone help as to why this error is being caused as i can see the playername.leaderstats.Stage value in explorer but in the script it cant find it any help would be appreciated Josh Edit: fix code edit 2: only include references to leaderstats
You forgot the WaitForChild incase the instance isn't yet created / loaded in yet. Also try Stage.Value?
-- >> Checkpoint Script local datastoreservice = game:GetService("DataStoreService") local spawns = datastoreservice:GetDataStore("spawns") script.Parent.Touched:Connect(function(hit) local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid") if humanoid then local player1 = game.Players:GetPlayerFromCharacter(hit.Parent) spawns:SetAsync(player1.userId, encodeCFrame(player1.Character:WaitForChild("HumanoidRootPart").CFrame)) -- printTable(player1.leaderstats:GetChildren()) player1.leaderstats:WaitForChild("Stage") = script.Parent.Name end end)
I found the issue. It's this:
game:GetService("Players")[player1.Name].leaderstats["Stage"] = script.Parent.Name
You are treating Stage
as like a property, however this fails as it's not a property.
Just append a .Value
.
Also this code can severely be cleaned.
First off, why go backwards to game:GetService("Players")
only to index it for player1.Name
?
This could be simplified to
player1.leaderstats.Stage.Value = script.Parent.Name
Mostly I answered because I wanted to clean up your printTable
function.
It can be as simple as...
local function print_table(t) print(string.format("{%s}", table.concat(t, ", "))) end
I would clean up your code, however it's too messy and beyond cleaning. You may need to restructure and rewrite parts of your code base to clean it up.