Basically the issue is not happening when loading in normal play test but it does happen in Local Server server. i don’t know what causing the issue and how i can fix it.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = false
local success,errorm = pcall(function()
DataStore = DataStoreService:GetDataStore("MoneyFtMiles")
end)
game.Players.PlayerAdded:Connect(function(player)
local getsuccess,result = pcall(function()
return DataStore:GetAsync(player.UserId)
end)
if not getsuccess and result then
warn("Load failed for Money and miles : "..result)
player:Kick("Something doesn't allow you're stats to properly load at the time. Please rejoin")
return
end
local groupsuccess,grouperror = pcall(function()
local group = player:IsInGroup(1)
end)
if not groupsuccess and grouperror then
warn("Roblox might be down : "..grouperror)
player:Kick("Roblox might be down at the moment, please join later to protect your progress.")
return
end
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local Money = Instance.new("NumberValue",leaderstats)
Money.Name = "Money"
Money.Value = result.Money
local Miles = Instance.new("NumberValue",leaderstats)
Miles.Name = "Miles"
Miles.Value = result.Miles
end)
game.Players.PlayerRemoving:Connect(function(player)
if not player:FindFirstChild("leaderstats") then
return
end
local Money = player.leaderstats:FindFirstChild("Money")
local Miles = player.leaderstats:FindFirstChild("Miles")
if not Money or not Miles then
return
end
success,errorm = pcall(function()
DataStore:SetAsync(player.UserId,{
["Money"] = Money.Value;
["Miles"] = Miles.Value;
})
end)
if not success then
warn("SAVING FAILLURE : "..errorm)
end
end)
workspace.ChildAdded:Connect(function(child)
local player = game.Players:GetPlayerFromCharacter(child)
if not player then
return
end
for _,part in child:GetDescendants() do
if part:IsA("Part") or part:IsA("MeshPart") then
part.CollisionGroup = "Players"
end
end
end)
This is because the script is attempting to find Money in an instance that is nil, which turns out to be leaderstats. Try waiting for the player’s character or do game.Loaded:Wait() at the top to see if it helps.
Another great trick, always set the attributes of an instance first before setting its parent, that way it’s more efficient.
-- Bad practice, setting parent before setting attributes meaning server has to connect and fire additonal events.
local Money = Instance.new("NumberValue",leaderstats)
Money.Name = "Money"
Money.Value = result.Money
-- Good practice, mitigates any unnecessary steps the server has to take.
local Money = Instance.new("NumberValue")
Money.Name = "Money"
Money.Value = result.Money
Money.Parent = leaderstats
After modifying the script the issue seems to be happening and result.Money and result.Miles does exist
local DataStoreService = game:GetService("DataStoreService")
local DataStore = false
local success,errorm = pcall(function()
DataStore = DataStoreService:GetDataStore("MoneyFtMiles")
end)
game.Players.PlayerAdded:Connect(function(player)
local getsuccess,result = pcall(function()
return DataStore:GetAsync(player.UserId)
end)
task.wait(.05)
if not getsuccess and result then
warn("Load failed for Money and miles : "..result)
player:Kick("Something doesn't allow you're stats to properly load at the time. Please rejoin")
return
end
if getsuccess then
if not result.Money or not result.Miles then
for i,v in result do
print(i,v)
end
warn("Something went wrong load money")
return
end
end
local groupsuccess,grouperror = pcall(function()
local group = player:IsInGroup(1)
end)
if not groupsuccess and grouperror then
warn("Roblox might be down : "..grouperror)
player:Kick("Roblox might be down at the moment, please join later to protect your progress.")
return
end
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local Money = Instance.new("NumberValue")
Money.Name = "Money"
Money.Value = result["Money"] or 0
Money.Parent = leaderstats
local Miles = Instance.new("NumberValue")
Miles.Name = "Miles"
Miles.Value = result["Miles"] or 0
Miles.Parent = leaderstats
end)
game.Players.PlayerRemoving:Connect(function(player)
if not player:FindFirstChild("leaderstats") then
return
end
local Money = player.leaderstats:FindFirstChild("Money")
local Miles = player.leaderstats:FindFirstChild("Miles")
if not Money or not Miles then
return
end
success,errorm = pcall(function()
DataStore:SetAsync(player.UserId,{
["Money"] = Money.Value;
["Miles"] = Miles.Value;
})
end)
if not success then
warn("SAVING FAILLURE : "..errorm)
end
end)
workspace.ChildAdded:Connect(function(child)
local player = game.Players:GetPlayerFromCharacter(child)
if not player then
return
end
for _,part in child:GetDescendants() do
if part:IsA("Part") or part:IsA("MeshPart") then
part.CollisionGroup = "Players"
end
end
end)
actually i found the issue, look closely, the stat dont create because its empty, result is nil because player don’t have saved stats, solved by letting a new table handle the stat for players.