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

datastore doesnt save player data?

Asked by 2 years ago
Edited 2 years ago

my leaderboard script doesnt save my minutes after i buy something, it reverts back to however many i had before i bought the item for however many minutes

local DataStoreService = game:GetService("DataStoreService") local DataStore = DataStoreService:GetDataStore("TimeStats")

game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player

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

local UserID = "Player_".. player.UserId

local Data
local success, errormessage = pcall(function()
    Data = DataStore:GetAsync(UserID)
end)
if success and Data then

    Minutes.Value = Data.Minutes

end

coroutine.resume(coroutine.create(function()
    while true do
        wait(60)
        Minutes.Value = Minutes.Value + 1
    end
end))

end)

game.Players.PlayerRemoving:Connect(function(player) local UserID = "Player_".. player.UserId

local minutes = player.leaderstats.Minutes

local success, errormessage = pcall(function()
    DataStore:SetAsync(UserID, minutes)
end)

if success then
    print("Data Successfully Saved")
else
    print("Could Not Save Data")
    warn (errormessage)
end

end)

here is my buy script, its copy pasted and altered for other items but this is the basic template

local player = game.Players.LocalPlayer local croissant = game.ReplicatedStorage.Croissant local backpack = player.Backpack local remote = game.ReplicatedStorage.Buy

script.Parent.MouseButton1Click:Connect(function() if player.leaderstats.Minutes.Value >= 2 then player.leaderstats.Minutes.Value -= 2 croissant:Clone() croissant.Parent = backpack script.Disabled = true else script.Parent.Parent.Parent.Parent.Parent.ErrorMessage.Visible = true wait(2) script.Parent.Parent.Parent.Parent.Parent.ErrorMessage.Visible = false end

end)

0
Could you show the script which handles buying? Mafincho 43 — 2y
0
Hey there, I received your message on Roblox! Mafincho 43 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

I suppose this is a local script since you are reaching the LocalPlayer. Local scripts are not synced with the server. What I suggest doing is using a remote event that fires from the LS (local script) to the SS (server script). Then the SS accepts the event and subtracts the user's minutes there. Hope this helps!

Ad

Answer this question