DataStore is saving other players stats and not my stat

Hi.

My datastore has a bug. When a player leaves the server, they are supposed to keep their stat. But it’s not like that. When there are several players in the server it saves the stat of another player. Any way to fix this?

Here is my script:

local datastore = game:GetService("DataStoreService")
local data1 = datastore:GetDataStore("myhoneydata")
local data2 = datastore:GetDataStore("myticketdata")

game.Players.PlayerAdded:Connect(function(pl)
	local ls = Instance.new("Folder", pl)
	ls.Name = "leaderstats"
	local bb = Instance.new("Folder", pl)
	bb.Name = "BuyedBees"
	local po = Instance.new("IntValue", ls)
	po.Name = "Pollen"
	local ho = Instance.new("IntValue", ls)
	ho.Name = "Honey"
	local ti = Instance.new("IntValue", ls)
	ti.Name = "Ticket"
	
ho.Value = data1:GetAsync(pl.UserId) or 0
data1:SetAsync(pl.UserId, ho.Value)
ti.Value = data2:GetAsync(pl.UserId) or 0
data2:SetAsync(pl.UserId, ti.Value)

game.Players.PlayerRemoving:Connect(function(pl)
data1:SetAsync(pl.UserId, ho.Value)
data2:SetAsync(pl.UserId, ti.Value)
end)
end)

My game: https://www.roblox.com/games/2959047441/OOF-Swarm-Simulator-Remake
1 player per server until I can fix this bug.

2 Likes

Please post your code properly, it will help other developers solve your issue. Please read this post to learn more about adding code to your posts on the Developer Forum

Inserting Code Snippets

1 Like

It is not good to have a PlayerRemoved function inside a PlayerAdded one. Make a separate function and find the values.

4 Likes

This script should be enough for you to at least have a working data store script.

local DataStoreService = game:GetService("DataStoreService")

local Players = game:GetService("Players")

local function OnPlayerAdded(Client)
	
	local DataStore = DataStoreService:GetDataStore("Player_Data",Client.UserId)
	
	local LeaderstatsFolder = Instance.new("Folder")
	local Currency_Folder = Instance.new("Folder")
	local Currency_1 = Instance.new("IntValue")
	local Currency_2 = Instance.new("IntValue")
	local Currecny_3 = Instance.new("IntValue")
	
	LeaderstatsFolder.Name = "leaderstats"
	Currency_Folder.Name = "Bought Bees"
	Currency_1.Name = "Pollen"
	Currency_2.Name = "Honey"
	Currecny_3.Name = "Tickets"
	
	Currency_1.Value = 0
	Currency_2.Value = 0
	Currecny_3.Value = 0
	
	LeaderstatsFolder.Parent = Client
	Currency_Folder.Parent = Client
	Currency_1.Parent = LeaderstatsFolder
	Currency_2.Parent = LeaderstatsFolder
	Currecny_3.Parent = LeaderstatsFolder
	
	
	pcall(function()
		local PlayerData = DataStore:GetAsync("PlayerData")
		
		if PlayerData then
			for i,v in pairs(PlayerData) do
				if i == "Pollen" then
					Currency_1.Value = v
					
				elseif i == "Honey" then
					Currency_2.Value = v
					
				elseif i == "Tickets" then
					Currecny_3.Value = v
					
				else
					warn("An error has occured.")
				end
			end
		end
		
	end)
	
end

local function OnPlayerRemoving(Client)
	
	local DataStore = DataStoreService:GetDataStore("Player_Data",Client.UserId)
	
	local LeaderstatsFolder = Client:WaitForChild("leaderstats")
	local Currency_1 = LeaderstatsFolder:WaitForChild("Pollen")
	local Currency_2 = LeaderstatsFolder:WaitForChild("Honey")
	local Currecny_3 = LeaderstatsFolder:WaitForChild("Tickets")
	
	local PlayerData = {
		["Pollen"] = Currency_1.Value,
		["Honey"] = Currency_2.Value,
		["Tickets"] = Currecny_3.Value
	}
	
	pcall(function()
		DataStore:SetAsync("PlayerData", PlayerData)
	end)
	
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
2 Likes

Thanks but another person helped me. And thanks for helping me with code in posts.

1 Like

Requested by topic creator.

We do not lock support requests on request of topic creator because other users might have more input to provide or the solution might change over time.

1 Like