Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Saving Data Persistence issue?

Asked by
jtefurd 50
9 years ago

My script has been working for quite a while, but recently I've had to flip variables around and junk to make the script is cleaner. After I did, I got a strange error: Workspace.Leaderboard Script:28: attempt to index global 'Cash' (a nil value) what could this mean? My appreciation in advanced for any help you can give.

game.Players.PlayerAdded:connect(function(padd)

--Variables
local PlayerBag=Instance.new("Configuration",padd)
local LeaderStats=Instance.new("Model",padd)
local Cash=Instance.new("IntValue",LeaderStats)
local item1=Instance.new("BoolValue",PlayerBag)

--Names
PlayerBag.Name="PlayerBag"
LeaderStats.Name="leaderstats"
Cash.Name="Cash"
item1.Name="Item1"

--DataReady
padd:WaitForDataReady()
Cash.Value = padd:LoadNumber("Cash")
item1.Value = padd:LoadNumber("Item1")
end)

--DataSave
game.Players.PlayerRemoving:connect(function(premove)
local leaderstats=premove:FindFirstChild("leaderstats")
local playerbag=premove:FindFirstChild("PlayerBag")
if leaderstats then
local cash=leaderstats:FindFirstChild("Cash")
if cash then
premove:SaveNumber("Cash",Cash.Value)
end
end
if playerbag then
    local i1=PlayerBag:FindFirstChild("item1")
if i1 then
    premove:SaveBoolean("Item1",i1.Value)
end
end
end)

--CashGeneration
while wait(1) do
for _,Player in pairs(game.Players:GetPlayers()) do
if Player:FindFirstChild("leaderstats") then
Player.leaderstats.Cash.Value=Player.leaderstats.Cash.Value +1
end
end
end
1
Use Datastore not Data Persistence FearMeIAmLag 1161 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

Try this instead

game.Players.PlayerAdded:connect(function(padd)

--Variables
local PlayerBag=Instance.new("Configuration",padd)
local LeaderStats=Instance.new("Model",padd)
local Cash=Instance.new("IntValue",LeaderStats)
local item1=Instance.new("BoolValue",PlayerBag)

--Names
PlayerBag.Name="PlayerBag"
LeaderStats.Name="leaderstats"
Cash.Name="Cash"
item1.Name="Item1"

--DataReady
padd:WaitForDataReady()
Cash.Value = padd:LoadNumber("Cash")
item1.Value = padd:LoadNumber("Item1")
end)

--DataSave
game.Players.PlayerRemoving:connect(function(remove)
local leaderstats=remove:FindFirstChild("leaderstats")
local playerbag=remove:FindFirstChild("PlayerBag")
if leaderstats then
local cash=leaderstats:FindFirstChild("Cash")
if cash then
remove:SaveNumber("Cash",Cash.Value)
end
end
if playerbag then
    local i1=PlayerBag:FindFirstChild("item1")
if i1 then
    remove:SaveBoolean("Item1",i1.Value)
end
end
end)

--CashGeneration
while wait(1) do
for _,Player in pairs(game.Players:GetPlayers()) do
if Player:FindFirstChild("leaderstats") then
Player.leaderstats.Cash.Value=Player.leaderstats.Cash.Value +1
end
end
end

Ad

Answer this question