Datastore not saving as intended

I’m trying to make a skin system, for whenever someone enters a code, it will be a new skin added into their inventory and saves normally. To confirm, the code is being detected as well.

What am I trying to achieve?
A datastore that saves a table with the codes entered inside, checks if the player owns it, then shows that they own it.

What have I tried?
Print statements, looking to see if someone has encountered a issue likewise, seeing documentation on datastore.

task.wait()
local dataStoreService = game:GetService("DataStoreService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local completeSoundEvent = replicatedStorage:FindFirstChild("CompleteSound")
local dataStore = dataStoreService:GetDataStore("OwnedSkins")
local plrs = game:GetService("Players")

local contentProvider = script.Parent.Parent:FindFirstChild("ContentProvider")
local button = script.Parent
local player:Player = script.Parent.Parent.Parent.Parent.Parent
local saved = {}

while not player do
	task.wait()
end

local function getData(plr:Player)
	local userId = plr.UserId
	if userId then
		print(tonumber(userId))
		for i=1,#saved do
			print(saved[i])
		end
		return dataStore:GetAsync(plr.UserId) or saved
	end
end

local function saveData(plr:Player)
	local userId = plr.UserId
	if userId then
		print(tonumber(userId))
		for i=1,#saved do
			print(saved[i])
		end
		dataStore:SetAsync(plr.UserId,saved)
	end
end

local function addSkin(plr:Player,code:string)
	local userId = plr.UserId
	if code then
		print("Successfully Ran 1")
		local plrData = getData(plr)
		if table.find(saved,code) then
			return
		end
		print("Successfully Ran 2")
		if code == "AimbotLovesCoffee" then
			local codeItemContent = contentProvider:FindFirstChild("NonAimbotUser")
			table.insert(saved,code)
			codeItemContent.CanEquip.Value = true
			completeSoundEvent:FireClient(plr)
		elseif code == "TEHEPIKDUCKISCOMING" then
			local codeItemContent = contentProvider:FindFirstChild("Cazerdus")
			table.insert(saved,code)
			codeItemContent.CanEquip.Value = true
			completeSoundEvent:FireClient(plr)
		elseif code == "SUB2ARY0O51" then
			local codeItemContent = contentProvider:FindFirstChild("ARYOO")
			table.insert(saved,code)
			codeItemContent.CanEquip.Value = true
			completeSoundEvent:FireClient(plr)
		elseif code == "KittyClan" then
			local codeItemContent = contentProvider:FindFirstChild("KittyGuy")
			table.insert(saved,code)	
			codeItemContent.CanEquip.Value = true
			completeSoundEvent:FireClient(plr)
		end
	end
end

local function click()
	local code = button.Parent:FindFirstChild("Code").Value
	if code and player then
		print(`CODE ENTERED: {tostring(code)}`)
		addSkin(player,code)
	end
end

plrs.PlayerRemoving:Connect(function(plrRemoving:Player)
	print("Player Leaving")
	if plrRemoving.UserId == player.UserId then
		saveData(player)
	end
end)

game:BindToClose(function()
	saveData(player)
end)

button.MouseButton1Click:Connect(click)
local playerCurrentData = getData(player)
saved = playerCurrentData
for _,skinCode in pairs(saved) do
	print("CODE: "..tostring(skinCode))
	addSkin(player,tostring(skinCode))
end
6 Likes