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

Issues with datastores, maybe due to pcall?

Asked by 4 years ago

Hello! Im trying to save player data, and am having issues with saving it. It doesn't run the pcalls at all, and wondered if im missing something?

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("PlayerStatistics")

game.Players.PlayerAdded:Connect(function(player)
    print("PlayerAdded")
    local Statistics = Instance.new("Folder", player); Statistics.Name = "Statistics"
    local Money = Instance.new("IntValue", Statistics); Money.Name = "Money" 
    local Population = Instance.new("IntValue", Statistics); Population.Name = "Population" 
    local Souls = Instance.new("IntValue", Statistics); Souls.Name = "Souls" 

    local MoneyLoaded
    local PopulationLoaded
    local SoulsLoaded
    local success, errormessage = pcall(function()
        MoneyLoaded = ds:GetAsync(player.UserId.."-money")
        PopulationLoaded = ds:GetAsync(player.UserId.."-population")
        SoulsLoaded = ds:GetAsync(player.UserId.."-souls")
    end)        
    if success then
        Money.Value = MoneyLoaded
        Population.Value = PopulationLoaded
        Souls.Value = SoulsLoaded
        print(player.Statistics.Money.Value)
        print(player.Statistics.Population.Value)
        print(player.Statistics.Souls.Value)
    else
        print("Error occured during data get")
        warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    print("PlayerRemoved")
    local success, errormessage = pcall(function()
        ds:SetAsync(player.UserId.."-money", player.Statistics.Money.value)
        ds:SetAsync(player.UserId.."-population", player.Statistics.Population.value)
        ds:SetAsync(player.UserId.."-souls", player.Statistics.Souls.value)
        print(player.Statistics.Money.Value)
        print(player.Statistics.Population.Value)
        print(player.Statistics.Souls.Value)
    end)

    if success then
        print("Data saved successfully")
    else
        print("An error occured")
        warn(errormessage)
    end
end)

0
It gets up to the :SetAsync part, then stops PoppyandNeivaarecute 134 — 4y
0
You are doing way too much scripting here. It should only take like 25 lines of code, try using a tutorial. PrismaticFruits 842 — 4y
0
I did use a tutorial from yt! PoppyandNeivaarecute 134 — 4y
0
Also I added a bunch of prints for debugging PoppyandNeivaarecute 134 — 4y
View all comments (3 more)
0
try uppercase .Value royaltoe 5144 — 4y
0
Wdym? Where? PoppyandNeivaarecute 134 — 4y
0
Wdym? Where? PoppyandNeivaarecute 134 — 4y

Answer this question