Hey guys!! Haven’t been here in a while, I’ve been working on a game lately mainly building everything, but today I figured I would start some of the scripting for the game. Just my luck, of course the first thing I do, the leaderstats script is what doesn’t work, and I feel as if all my posts are about leaderstats it works, just wont save for some reason. I’ve tried multiple different scripts and different ways for it \ because of this and nothing wants to work. Can anyone help?
Heres the saving part of the script!
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Coins.Value = Data.Coins
Challenges.Value = Data.Challenges
else
print("There Was An Error Saving"..Player.UserId)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
["Coins"] = Player.leaderstats.Coins.Value;
["Challenges"] = Player.leaderstats.Challenges.Value;
})
end)
Try putting your save lines in a pcall, it may reveal some errors
local success, message = pcall(function()
Data = DataStore:GetAsync(Player.UserId)
end
if not success then
warn(message)
if Data then
Coins.Value = Data["Coins"]
Challenges.Value = Data["Challenges"]
else
print("There Was An Error Saving"..Player.UserId)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local success, message = pcall(function()
DataStore:SetAsync(Player.UserId, {
["Coins"] = Player.leaderstats.Coins.Value;
["Challenges"] = Player.leaderstats.Challenges.Value;
})
end)
if not success then
warn(message)
end
end)
My mistake, I forgot to add an end) there. Also it’s fine if Data is underlined, it just means it doesn’t have local so it’s getting a little mad but it won’t affect your code.
Here’s the fixed script
local success, message = pcall(function()
Data = DataStore:GetAsync(Player.UserId)
end)
if not success then
warn(message)
end
if Data then
Coins.Value = Data["Coins"]
Challenges.Value = Data["Challenges"]
else
print("There Was An Error Saving"..Player.UserId)
end
game.Players.PlayerRemoving:Connect(function(Player)
local success, message = pcall(function()
DataStore:SetAsync(Player.UserId, {
["Coins"] = Player.leaderstats.Coins.Value;
["Challenges"] = Player.leaderstats.Challenges.Value;
})
end)
if not success then
warn(message)
end
end)
I’m wondering if maybe it is having this saving issue because both of the currencies are being given to the player by another script (when a “challenge” is completed)… but that doesnt make much sense
That other script is also a server script. I had tried it earlier in a local script for something else and I don’t think it worked, I can try it again for this.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("CurrencyStats")
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local hiddenstats = Instance.new("Folder", Player)
hiddenstats.Name = "hiddenstats"
hiddenstats.Parent = Player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = leaderstats
local Challenges = Instance.new("IntValue")
Challenges.Name = "Challenges"
Challenges.Value = 0
Challenges.Parent = leaderstats
local success, message = pcall(function()
Data = DataStore:GetAsync(Player.UserId)
end)
if not success then
warn(message)
end
print(Data)
if Data then
Coins.Value = Data["Coins"]
Challenges.Value = Data["Challenges"]
else
print("There Was An Error Saving"..Player.UserId)
end
game.Players.PlayerRemoving:Connect(function(Player)
local success, message = pcall(function()
DataStore:SetAsync(Player.UserId, {
["Coins"] = Player.leaderstats.Coins.Value;
["Challenges"] = Player.leaderstats.Challenges.Value;
})
end)
if not success then
warn(message)
end
end)
end)
Then heres the coin giver script, the second half was its own script but i merged them, just teleports the player
local Teleporter = game.Workspace.Island1.MainTeleporting
local Debounce = false
local TimeToWait = 10
amnt1 = 100
amnt2 = 1
function onTouched(part)
if Debounce == false then
local h = part.Parent:findFirstChild("Humanoid")
if (h~=nil) then
local thisplr = game.Players:findFirstChild(h.Parent.Name)
if (thisplr~=nil) then
local leaderstatsstats = thisplr:findFirstChild("leaderstats")
if (leaderstatsstats~=nil) then
local coins = leaderstatsstats:findFirstChild("Coins")
local Challenges = leaderstatsstats:FindFirstChild("Challenges")
if (coins~=nil) then
coins.Value = coins.Value + amnt1
if (Challenges~=nil) then
Challenges.Value = Challenges.Value + amnt2
Debounce = true
wait(TimeToWait)
Debounce = false
else
print("no money 4 u")
end
end
end
end
end
end
end
script.Parent.Touched:Connect(onTouched)
script.Parent.Touched:Connect(function(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if Player then
local Teleporting = Player.Character:FindFirstChild("CurrentlyTeleporting")
if not Teleporting then return end
if not Teleporting.Value then
Teleporting.Value = true
Player.Character.HumanoidRootPart.CFrame = Teleporter.CFrame + Vector3.new(0,5,0)
wait(3)
Teleporting.Value = false
end
end
end)
I think I found your issue
The function is actually :FindFirstChild(), with a capital F in the beginning, and because you misspelled it, the if statement after it will not reach its condition.
Make sure to fix that issue for all the lines there, I can see several of them.
I might be wrong, but it seems like you are trying to save a dictionary to a datastore. Try using HttpService:JSONEncode() and HttpService:JSONDecode() when you are saving and loading the data.
Here are some examples (sorry for the bad formatting)
local success, errormessage = pcall(function()
local Data = {
["Coins"] = Player.leaderstats.Coins.Value;
["Challenges"] = Player.leaderstats.Challenges.Value;
}
local EncodedData = game:GetService("HttpService"):JSONEncode(Data)
DataStore:SetAsync(Player.UserId, EncodedData)
end)
local success, message = pcall(function()
Data = DataStore:GetAsync(Player.UserId)
end)
if success then
DecodedData = game:GetService("HttpService"):JSONDecode(Data)
end
As far as I know, there is no issue with saving dictionaries to datatsore. I have made several datastore scripts using dictionaries and they worked completely fine.