Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why doesn't this save the number value?

There is a value named "Level" and its in a folder named "stats" Basically this script is supposed to save the value "Level" Why isn't it working???

wait(2)
local ds = game:GetService("DataStoreService"):GetDataStore("PlayerData")

function PlayerEntered(player)
	local stats = player.Backpack:WaitForChild("stats")
	
	
	level = stats.Level
	
	level.Value = ds:GetAsync("Level_"..player.UserId) or 1200
end

function PlayerLeaving(player) 
	local stats = player.Backpack:WaitForChild("stats")
	level = stats.Level
	
	ds:SetAsync("Level_"..player.UserId, level.Value)
end

game.Players.PlayerAdded:connect(PlayerEntered)
game.Players.PlayerRemoving:connect(PlayerLeaving)
--y it no save?

1 answer

You need to make the stats global too. Like this:

wait(2)
local ds = game:GetService("DataStoreService"):GetDataStore("PlayerData")

function PlayerEntered(player)
    stats = player.Backpack:WaitForChild("stats")


    level = stats.Level

    level.Value = ds:GetAsync("Level_"..player.UserId) or 1200
end

function PlayerLeaving(player) 
    local stats = player.Backpack:WaitForChild("stats")
    level = stats.Level

    ds:SetAsync("Level_"..player.UserId, level.Value)
end

game.Players.PlayerAdded:connect(PlayerEntered)
game.Players.PlayerRemoving:connect(PlayerLeaving)

This discussion is preserved as it was. New answers, comments, and votes are closed.