Data not saving

Why wont the data save??

local DataStoreService = game:GetService("DataStoreService")

local Data = DataStoreService:GetDataStore("TEST1")

function save(plr)
	Data:SetAsync(plr, 1)
	
	print("Me")
	
end








game.Players.PlayerAdded:Connect(function(plr)


local player = game.Players:GetUserIdFromNameAsync(plr)
	print(Data:GetAsync(player))


if Data:GetAsync(player) == 1 then
	game.ReplicatedStorage.Takentest.Value = true
		
	
	
	
	
	
	
end
	
end)

game.ReplicatedStorage.Valchange.OnServerEvent:Connect(function(player)
local plr = player.UserId
save(plr)
		
	
	
end)

The reason it doesn’t save is because you are setting the datastore key as a instance, that instance doesn’t exist anymore once you leave and isn’t the same when the game starts again. What you should do instead is something like this:

-- // Declared Services

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players");
local ReplicatedStorage = game:GetService("ReplicatedStorage");

-- // Private Variables

local Valchange = ReplicatedStorage:WaitForChild("Valchange", 1);

local Data = DataStoreService:GetDataStore("TEST1")

-- // Functions

function save(plr)
	Data:SetAsync(`{tostring(plr.UserId)}'s-Data`, 1)
	print("Me")
end

local function NewPlayer(player: Player)
	local PlayerData = Data:GetAsync(`{tostring(player.UserId)}'s-Data`, 1)
	if PlayerData == 1 then
		print("Yass") -- do smth
	end
end

local function ValchangeHandler(player: Player)
	save(player)
end
	
-- // RBXConnections

Valchange.OnServerEvent:Connect(ValchangeHandler)
Players.PlayerAdded:Connect(NewPlayer)

What I did was save the key as the players userid (only one exists)

1 Like