My Data Store Is not working?

I made a Datastore for My game and it is not saving when I change it.

(No explaining needed)

local DataStoreService = game:GetService (“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

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

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

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

local playerUserId = "player_"..player.UserId


-- Load Data

local data
local success, errormessage = pcall(function()
	 data = myDataStore:GetAsync(playerUserId)
end)



if success then 
	Loyalty.Value = data
	 -- Set our Data equal to the current Lotalty
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “player_”…player.UserId

local data = {
	Loyalty = player.leaderstats.Loyalty.Value;
}



local data = player.leaderstats.Loyalty.Value

local success, errormessage = pcall(function()
	myDataStore:SetAsync(playerUserId, data)
end)	

if  success then 
	print("Data successfuly saved!")
else
	print("There was an error!")
	warn(errormessage)
	end
	
end)
1 Like

You are changing it on the client, change it on the server

You can switch to the server by pressing the button that says “Current: Client”

2 Likes