Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

[SOLVED] My DataStore script isn't working and there are no errors?

Asked by
Sorukan 202 Moderation Voter
5 months ago
Edited 5 months ago

The output doesn't print any error, it's just that it never saves or loads the data because it always prints "New Player". I have the Studio Access to API Services enabled and i did publish the game so i can't see what's wrong.

--//Services
local dataStore = game:GetService('DataStoreService'):GetDataStore('DataStoreName') 

--//CurencyName
local currencyName = 'Gold'

--//PlayerAdded
game.Players.PlayerAdded:Connect(function(player)
    local folder = Instance.new('Folder')
    folder.Name = 'leaderstats' 
    folder.Parent = player

    local currency = Instance.new('IntValue')
    currency.Name = currencyName 
    currency.Parent = folder

    local ID = currencyName..'-'..player.UserId 
    local savedData = nil 

    pcall(function()
        local savedData = dataStore:GetAsync(ID)
    end)

    --//If player has data then load it
    if savedData ~= nil then
        currency.Value = savedData
        print('Data Loaded')
    else
        --//Else if player doesn't have data then give them 500 Gold
        currency.Value = 500
        print('New Player')
    end
end)
0
why do you need to wrap :GetAsync() in a pcall User#23365 -5 — 5mo
0
Well according to a video that i saw, it is used to prevent the script from breaking entirely if an error occurs. Sorukan 202 — 5mo
0
pcall will return the error for you. first it returns false, and then a message with what the error is. take a look here >> https://developer.roblox.com/articles/Built-in-Functions-and-Variables/Lua#pcall GoodCallMrOlsen 55 — 5mo
0
I removed the pcall function and it worked, i'm still not sure why the person in the video i watched used the pcall function, it worked for him though. Sorukan 202 — 5mo

Answer this question