Help needed to store data (DataStore2)

Help me pls

local DataStore = require(game.ServerScriptService.DataStore2)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats"
local SecoundsPlayed = Instance.new("IntValue", player) SecoundsPlayed.Name  = "SecoundsPlayed"
local TimeStats  = Instance.new("StringValue", leaderstats) TimeStats.Name = "Time"

local TimePlayedStore = DataStore("SecoundsPlayed".."1", player)

local function  TimeUpdate (UpdatedValue)
TimeStats.Value = TimePlayedStore:Get(UpdatedValue)
end

TimeUpdate(0)

TimePlayedStore:OnUpdate(TimeUpdate)


while true do
TimePlayedStore:Increment(1, player)
wait(1)
end
end)

Why can’t I recover the value when I leave the game? it always returns 0

1 Like

What’s the error(s) if any? You’ve provided absolutely no context, and what you’ve tried to recover this issue. How are we supposed to help you without this information?

4 Likes

Increment takes a number to add and then a default value if there’s no data saved to increment upon.
Instead of that default value, you’ve given the player.
Moreover, the DataStore is for that player anyways.


FYI: Don’t use the second argument of Instance.new, it’s quite inefficient - instead set the .Parent of your Instance. You can read more here:

1 Like

i suggest using :Get() and dont add player in commas because its already a Store for a specific value

1 Like

why are you deleting the indents that roblox automatically adds it makes code much harder to read