Why does my script load a lot of data that isn't mine? {Solved basically}

Hello, I am trying to make a script that will save data to a plr’s userId after they buy an item/boolVal (this way even if they leave the game they will still have their item) and then load it when the plr rejoins. The only issue is that when I try to load it after they join it loads alot of data that isn’t that plrs’s data. I have tried many different accounts and the same thing happens. Any ideas?

-- Save script
elseif v.Time.Value == 0 then
				v.CanBid.Value = false
				local Data = game:GetService("DataStoreService"):GetDataStore("ItemSaves"):GetAsync("ItemSaveData-save")
				
				local savedItems = game:GetService("DataStoreService"):GetDataStore("ItemSaves"):GetAsync("ItemSaveData-save2")
				
				if game.Players:FindFirstChild(v.Bidder.Value) then
					local plr = game.Players:FindFirstChild(v.Bidder.Value)
					local clone = Instance.new("BoolValue", plr)
					clone.Parent = plr
					clone.Name = v.Name
					clone.Value = true
				end
				
				table.insert(savedItems, {
					Name = v.Name
				})
				
				
				game:GetService("DataStoreService"):GetDataStore("ItemSaves"):SetAsync("ItemSaveData-save", savedItems, v.Bidder.Value.UserId)
				wait(1.5)
				v:Destroy()
				game.ServerStorage.ItemsCount.Value -= 1
			end```

```lua
-- load script
local Data = game:GetService("DataStoreService"):GetDataStore("ItemSaves"):GetAsync("ItemSaveData-save")
	
	if Data then
		-- Load all saved items
		for _, itemData in pairs(Data) do
			local Name = itemData.Name
			--if not plr:FindFirstChild(Name) then
				local clone = Instance.new("BoolValue", plr)
				clone.Parent = plr
				clone.Name = Name
				clone.Value = true
			--end
		end
	else
		game:GetService("DataStoreService"):GetDataStore("ItemSaves"):SetAsync("ItemSaveData-save", {}, plr.UserId)
		print("no data found")
	end
	
	local savedItems = game:GetService("DataStoreService"):GetDataStore("ItemSaves"):GetAsync("ItemSaveData-save")
	
	plr.ChildRemoved:Connect(function(child)
		if game.ServerStorage.AuctionItems:FindFirstChild(child.Name) then
			table.remove(savedItems, 1, {
				Name = child.Name
			})
			print("removed")
			game:GetService("DataStoreService"):GetDataStore("ItemSaves"):SetAsync("ItemSaveData-save", savedItems, plr.UserId)
		end
	end)```

Everything you save goes into one single global key “ItemSaveData-save”, so there’s nothing to uniquely identify a given player’s UserId. Include their UserId as part of the key.

1 Like

Where would I put their userId without it giving me an error?

SetAsync("ItemSaveData-save" .. plr.UserId)

local DataStoreService = game:GetService("DataStoreService")

local experienceStore = DataStoreService:GetDataStore("PlayerExperience")

local success, errorMessage = pcall(function()
	experienceStore:SetAsync("User_1234", 50)
end)
if not success then
	print(errorMessage)
end

Something like this for example, where the first parameter is the userID as the key.
From here:

1 Like

It attempts to concatenate string with nil when it’s GetAsyncing…

That means plr.UserId is returning nil. Check what your “plr” variable actually is

There is nothing wrong with the plr var. It is from game.Players.PlayerAdded or a string value. It will setAsync just fine but won’t getAsync.

Try printing out the plr and plr.UserId variable, because your error message says you’re trying to concat a string “ItemSaveData-save” with nil

Also this means if I try to get data from the string val the plr will have to be in that server/game, therefor defeating the whole pourpose.

Oh, I have found out that the reason I got an error was because the string value was containing a plr who wasn’t in the server, But that still means they will have to be in that server or in the game at the exact time it wants to give the item to the plr and save it.

Why do you have a string value in the first place? Seems like an error waiting to happen

For context I am making a server wide auction house and I store the player who bided the highest amount of money for that item in a string value. I then use messaging service to copy that value to all servers.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.