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

Why are the points not saving properly?

Asked by 4 years ago

Im trying to save the players stat points and it works nice, but for some reason its not working for melee everytime, here is the script:

local DataStore = game:GetService("DataStoreService"):GetDataStore("StatsDataStore")
game.Players.PlayerAdded:Connect(function(player)
    local MeleeName = "MeleeP"
    local folder = player:WaitForChild("Data")
    local MeleeP = player:FindFirstChild("Data"):WaitForChild("MeleeP")
    local ID2 = MeleeName.."-"..player.UserId
    local savedData = nil
    pcall(function()
        savedData = DataStore:GetAsync(ID2)
    end)

        if savedData ~= nil then
        MeleeP.Value = savedData
        print("Data loaded")
    else
        -- New player
        MeleeP.Value = 1
        print("New player to the game")
    end
end)
local MeleeName = "MeleeP"
game.Players.PlayerRemoving:Connect(function(player)
    local ID2 = MeleeName.."-"..player.UserId
    DataStore:SetAsync(ID2,player.Data[MeleeName].Value)
end)

game:BindToClose(function()

    -- When game is ready to shutdown

    for i, player in pairs(game.Players:GetPlayers()) do
        if player then
            player:Kick("This game is shutting down")
        end
    end
    wait(5) 
end)

Thank you!

0
Well first of all when you do find first child, it either finds it or it returns nil. That’s important to know. Wait for child waits for it indefinitely. Now you already said folder = the data part so this is just a tip.. when you declare the variable MeleeP you can say MeleeP = folder. Blah. That wasn’t the issue but it irked me. SethHeinzman 284 — 4y
0
i alredy solved it btw and thanks for the advice but a wait(4) after the player added event has the same effect valledestroy 54 — 4y
0
i also know that i could change that but im to lazy for that valledestroy 54 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Already Solved!

Ad

Answer this question