Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

DataStore not working and no error? Not a local script

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

game.Players.PlayerAdded:Connect(function(player)
    local PlayerUserId = "Player_"..player.UserId
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"
    local Credits = Instance.new("IntValue", leaderstats)
    Credits.Name = "Credits"
    local Research = Instance.new("IntValue", leaderstats)
    Research.Name = "Research"
    --Load Data
    
    local data
    local success, errormessage = pcall(function()
        data = DataStore1:GetAsync(PlayerUserId)
    end)
    if data ~= nil and success then
            data[Credits.Value] = Credits.Value
            data[Research.Value] = Research.Value
		print("21")
	elseif data == nil and success then
            data[Credits.Value] = 500
            data[Research.Value] = 10--setting it to the default amount if they dont have date
		end
    end)
	print("27")
game.Players.PlayerRemoving:Connect(function(player)
    local PlayerUserId = "Player_"..player.UserId
    print("30")
	local data = {}
    for i, v in pairs(player.leaderstats:GetChildren())do
        table.insert(data, v.value)
    end
	print("34")
    
    local success, errormessage = pcall(function()
        DataStore1:SetAsync(PlayerUserId,data)        
    end)
    
    if success then
        print("Data Successfully Saved")
    
    else
        print("There was an error saving data")
        warn(errormessage)
    
    end
end)

The problem might lie in lines between 27 and 34 which don't print the numbers

1 answer

✓ Accepted answer
local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1")

game.Players.PlayerAdded:Connect(function(player)
    local PlayerUserId = "Player_"..player.UserId
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"
    local Credits = Instance.new("IntValue", leaderstats)
    Credits.Name = "Credits"
    local Research = Instance.new("IntValue", leaderstats)
    Research.Name = "Research"
    --Load Data

    local data
    local success, errormessage = pcall(function()
        data = DataStore1:GetAsync(PlayerUserId)
    end)
    if data then
            Credits.Value = data[1]
            Research.Value = data[2]
        print("21")
    else
            Credits.Value = 500
            Research.Value = 10
        end
    end)

game.Players.PlayerRemoving:Connect(function(player)
    local PlayerUserId = "Player_"..player.UserId
    print("30")
    local data = {}
    for i, v in pairs(player.leaderstats:GetChildren())do
        table.insert(data, v.Value)
    end

    local success, errormessage = pcall(function()
        DataStore1:SetAsync(PlayerUserId,data)        
    end)

    if success then
        print("Data Successfully Saved")

    else
        print("There was an error saving data")
        warn(errormessage)

    end
end)

This should work, also when using studio PlayerRemoving doesn't always work so try changing your stats in your published game. If you need anymore help dont hesitate to contact me!

EDIT: Just changed some minor things, shouldn't change functionality.

1 comment

0

God Bless you! Thank you so much, spent 2 days trying to figure this out, thank you again.

This discussion is preserved as it was. New answers, comments, and votes are closed.