Recently I switched from Data Persistence to Data Storage, and my level up script will not work until the player has reset after the first time joining, and I get errors saying "this is not a valid member of that" Until the player has reset. So I thought about instead of adding WaitForChilds in all of my scripts, I would just add a simple script that makes the player die and be forced to re-spawn when they join the game, to ensure that everything works right. Here is my attempt, which I could not get to work:
game:GetService('Players').PlayerAdded:connect(function(player)
player.Character.Health = 0
end
My attempt is so bad that it almost seems lazy to me, but then again, I've never been faced with this situation.
Any help is greatly appreciated, thank you.
Edit: Here is that Data Storage script and the Level up script
Data Storage:
local DSService = game:GetService('DataStoreService'):GetDataStore('Stats')
ID = 68
SaveTime = 60
leaderboardname = "Levelss" --The name of the leader board
StatList = {Muny = 250 , Level = 1 , XP = 0 , FAP = 1 , RandomMove = 0 , RangedPunch = 0 }
function CreateValue(Name, Parent, Value) --If value is set to a number then new players will start with that amount.
Value = Value or 0
local savevalue = Instance.new('IntValue', Parent)
savevalue.Name = Name
savevalue.Value = Value
return savevalue
end
function GetValue(uniquekey)
-- GetAsync
local GetSaved = DSService:GetAsync(uniquekey)
if GetSaved then
return GetSaved
end
end
game.Players.PlayerAdded:connect(function(plr)
local uniquekey = ID..plr.userId
local Levelss = Instance.new('IntValue', plr)
Levelss.Name = leaderboardname
for i,v in pairs(StatList) do
local Stat = GetValue(i..uniquekey)
if Stat then
CreateValue(i, Levelss, Stat) --Old player load their value
else
CreateValue(i, Levelss, v) --New player load the default value
end
end
end)
game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = ID..plr.userId
if plr:FindFirstChild(leaderboardname) then
for i,v in pairs(StatList) do
if plr[leaderboardname]:FindFirstChild(i) then
DSService:SetAsync(i..uniquekey, plr[leaderboardname][i].Value)
end
end
end
end)
while wait(SaveTime) do
P = game:GetService("Players"):GetChildren()
for o=1, #P do
if P[o] then
local uniquekey = ID..P[o].userId
if P[o]:FindFirstChild(leaderboardname) then
for i,v in pairs(StatList) do
if P[o][leaderboardname]:FindFirstChild(i) then
DSService:SetAsync(i..uniquekey, P[o][leaderboardname][i].Value)
end
end
end
end
end
end
Level up: (isnt 100% finished but it does still modify level so it's good enough for testing)
s = script.Parent
p = s.Parent
c = p.Character
p.Levelss.XP.Changed:connect(function()
if p.Levelss.XP.Value >= p.Levelss.Level.Value * 20 then
p.Levelss.XP.Value = 0
p.Levelss.Level.Value = p.Levelss.Level.Value + 1
p.Character.Humanoid.MaxHealth = p.Character.Humanoid.MaxHealth + 10
p.Character.Humanoid.Health = p.Character.Humanoid.Health + 10
p.Levelss.FAP.Value = p.Levelss.FAP.Value + 1
end
end)

6 comments
Why do you need to reset the player? can you include the level up script.
Basically the Data does not load before the character is added, which is why it needs to be reset. But, nothing will work until the character is reset. Scripts cant find values, for example, Player.Data.Level would come up as (Level is an invalid member blah blah) Resetting fixes all of that, and I'd rather just reset the player for now, and bother with fixing the whole thing later on down the roa
A data store does not need the player to save / load data
Can you include the full script.
There is the data storage and level up script
why not use :BreakJoints() on the Character?