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

DataStore script running without error however not saving?

Asked by
5xephos 15
4 years ago
Edited 4 years ago

I'm a relatively new at scripting and began not so long ago, I watched some videos on YouTube on how to create a DataStore and such to save data. I've been testing it in the Studio Test server with only 1 player, and it's a normal script located in ServerScriptService. When I run the test server, it shows no errors in the output, I've tried using prints in between the script to see if I can pinpoint where the problem is however, it runs straight through with the prints and all of them show up in the output What i'm trying to achieve with the script is that I want it to save a certain value (the rank of the player) so when they rejoin it didn't change from when they last played however, when I join the test server, I raise my players rank to 1, rejoin and its back at 0

This is the code.

local LevelDataStoreService = game:GetService("DataStoreService")
local levelDataStore = LevelDataStoreService:GetDataStore("levelDataStore")

game.Players.PlayerAdded:Connect(function(player)
wait(1)
print("player added")
local stats = player.leaderstats or player:WaitForChild("leaderstats")

local rank = Instance.new("IntValue")
rank.Name = "Rank"
rank.Parent = stats

local exp = Instance.new("IntValue")
exp.Name = "EXP"
exp.Parent = stats

local expneeded = Instance.new("IntValue")
expneeded.Name = "EXPN"
expneeded.Parent = stats
expneeded.Value = (rank.Value * 5.65)


    local dataRank

local success, errormessage = pcall(function()
    dataRank = levelDataStore:GetAsync(tostring(player.UserId.."-rank"))
    print("pcall went through")
end)
if success then
    rank.Value = dataRank
    print("Data loaded for levels")
else
    print("There was an error getting rank data")
    warn(error)
end
end)

game.Players.PlayerRemoving:Connect(function(player)
local stats = player.leaderstats
local rank = stats.Rank
local success, errormessage = pcall(function()
    levelDataStore:SetAsync(player.UserId.."-rank",rank.Value)
end)

if success then
    print("Player level data saved")
else
    print("Player level error occured saving data")
    warn(errormessage)
end

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 server is shutting down")

wait(5)




        end
    end
end)

1 answer

Log in to vote
0
Answered by
5xephos 15
4 years ago

Figured out my error, the server didn't see the changes in the Rank value but the client did so it didn't save any changes because the server didn't see any. Seems I have to use RemoteFunctions/RemoteEvents to fix this error

Ad

Answer this question