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

Why is the code no saving or loading is working right?

Asked by 5 years ago
Edited 5 years ago

``local currencyName = "Coins"

local DataStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

game.Players.PlayerAdded:Connect(function(player)

01local folder = Instance.new("Folder")
02folder.Name = "leaderstats"
03folder.Parent = player
04 
05local currency = Instance.new("IntValue")
06currency.Name = currencyName
07currency.Parent = folder
08 
09local ID = currencyName.."-"..player.UserId
10local savedData = nil
11 
12pcall(function()
13    local savedData = DataStore:GetAsync(ID)
14end)
15 
View all 21 lines...

end)

game.Players.PlayerRemoving:Connect(function(player)

1local ID = currencyName.."-"..player.UserId
2 
3DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)

end)

game:BindToClose(function()

1for i, player in pairs(game.Players:GetPlayers())do
2 
3    if player then
4 
5        player:Kick("this game is shutting down")
6    end
7end
8 
9wait(5)

end)

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Make sure to accept this answer if it solved your problem!

Question

Why is the code no saving or loading is working right?

Problem

In your pcall statement you define a new function. Inside that function you define a new variable savedData. The local keyword creates a variable in the current scope. You're currently creating the variable inside the scope of the anonymous function you passed to pcall.

Solution

Simply remove the local from the savedData declaration inside the pcall.

Example Code

1local savedData = nil
2 
3pcall(function()
4    savedData = DataStore:GetAsync(ID)
5end)

Extra Resources

http://lua-users.org/wiki/ScopeTutorial

0
It is not working it should be able to work right? Staven2u 2 — 5y
Ad

Answer this question