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

My datastore script does not work for unknown reason, I get no errors?

Asked by 1 year ago
local dataStoreService = game:GetService("DataStoreService")
local playerData = dataStoreService:GetDataStore("PlayerData")

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

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

    local Experience = Instance.new("StringValue")
    Experience.Parent = leaderstats
    Experience.Name = "Experience"


    local SurvivalTime = Instance.new("IntValue")
    SurvivalTime.Parent = leaderstats
    SurvivalTime.Name = "Survived"

    local Money = Instance.new("IntValue")
    Money.Parent = leaderstats
    Money.Name = "BackDollars"  

    player.CharacterAdded:Connect(function(character)
        local humanoid = character:FindFirstChild("Humanoid")

            humanoid.Died:Connect(function(died)
                local tag = humanoid:FindFirstChild("creator")
                local killer = tag.Value
                if tag and killer   then
                    killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("Kills").Value + 1
                    killer.leaderstats:FindFirstChild("BackDollars").Value += 1
                end
            end)
    end)
    local playerUserId = "Player_"..player.UserId

    local data = playerData:GetAsync(playerUserId)
    if data then
        kills.Value = data["Kills"]
        Experience.Value = data["Experience"]
        SurvivalTime.Value = data["Survived"]
        Money.Value = data["BackDollars"]
    else
        kills.Value = 0
        Experience.Value = "Newcomer"
        SurvivalTime.Value = 0
        Money.Value = 0
        print("datastore")
    end
end)

local function create_table(player)
    local player_stats = {}
    for i, stat in pairs(player.leaderstats:GetChildren()) do
        player_stats[stat.Name] = stat.Value
    end
    return player_stats
end

local function SaveData(player)
    local player_stats = create_table(player)
    local success, err = pcall(function()
        local playerUserId = "Player_"..player.UserId
        playerData:SetAsync(playerUserId, player_stats)

    end)

    if not success then
        warn("Could not save data")
    end
end
function SaveAllData()
    for _,player in pairs(game.Players:GetPlayers()) do
        SaveData(player)
    end
end
game:BindToClose(function()
    SaveAllData()
end)

while true do
    SaveAllData()
    wait(60)
end
--game.Players.PlayerRemoving:Connect(onPlayerExit)



2 answers

Log in to vote
1
Answered by 1 year ago

Hello, if you're testing in the studio test mode, this may not work. So try to publish and then get into the game.

0
Will try, thank you Sabailuridze 126 — 1y
0
Sadly still does not work, I will try to figure something out Sabailuridze 126 — 1y
0
IT GOT FIXED, idk how but It started working normally in game Sabailuridze 126 — 1y
Ad
Log in to vote
0
Answered by
enes223 327 Moderation Voter
1 year ago

hey you! have you ever heard of enes? if you are in trouble, better call enes!

Answer this question