Ad
Log in to vote
0

data isnt saving when leaving?

Asked by 15 days ago

my data for a game im working on isnt saving or not loading not sure. ive checked in studio and in the actual game and its not working

local serverStorage = game:GetService("ServerStorage")
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerSave")

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


local leaderstats = Instance.new("Folder")
leaderstats.Name= "leaderstats"
leaderstats.Parent = player

local wheat = Instance.new("NumberValue")
wheat.Name = "Wheat"
wheat.Parent = leaderstats

local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Parent = leaderstats

local dataFolder = Instance.new("Folder")
dataFolder.Name = player.Name
dataFolder.Parent = serverStorage.RemoteData    

local debounce = Instance.new("BoolValue")
debounce.Name = "Debounce"
debounce.Parent = dataFolder    

local wheatData, rebirthsData

local success, errormessage = pcall (function()
    wheatData = DataStore:GetAsync("wheat-")..player.UserId
    rebirthsData = DataStore:GetAsync("rebirths-")..player.UserId
end)

if success then
    if wheatData then
        wheat.Value = wheatData
        rebirths.Value = rebirthsData
    end
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
    DataStore:SetAsync("wheat-"..player.UserId,player.leaderstats.Wheat.Value)
    DataStore:SetAsync("rebirths-"..player.UserId,player.leaderstats.rebirths.Value)
end)
end)
0
have you tried using prints to debug or looked at the output for errors? cmgtotalyawesome 1403 — 15d
0
Hey i edited the thing now u just can copy and paste the script into serverscriptservice xGlacier101x 9 — 12d

1 answer

Log in to vote
0
Answered by 15 days ago
Edited 12 days ago

don't use that use this instead

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")

local function onPlayerJoin(player) -- Runs when players join

local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

leaderstats.Name = "leaderstats"

leaderstats.Parent = player



local money = Instance.new("IntValue") --Sets up value for leaderstats

money.Name = "Wheat"

money.Parent = leaderstats

local shards = Instance.new("IntValue") --Sets up value for leaderstats

RB.Name = "Rebirths"

RB.Parent = leaderstats


local playerUserId = "Player_" .. player.UserId  --Gets player ID

local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

if data then

    money.Value = data['Wheat']

    RB.Value = data['Rebirths']

    print('Dataloaded!')

else

    -- Data store is working, but no current data for this player

    money.Value = 0

    RB.Value = 0

end

end

local function create_table(player)

local player_stats = {}

for _, stat in pairs(player.leaderstats:GetChildren()) do

    player_stats[stat.Name] = stat.Value

end

return player_stats

end

local function onPlayerExit(player) --Runs when players exit

local player_stats = create_table(player)

local success, err = pcall(function()

    local playerUserId = "Player_" .. player.UserId

    playerData:SetAsync(playerUserId, player_stats) --Saves player data

end)



if not success then

    warn('Could not save data!')

end

end

game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

Ad

Answer this question