Data Store Script Broke

Hello! I’m trying to make it so when the player collects coins then, it saves but that isn’t working in my game and the coin value straight up resets, also nothings coming up inside the output. Here’s the game if you wanna test my changes out,

– If you wanna see the Console/Output then say “/Console”

https://www.roblox.com/games/6865039231/Super-Oofy-64

Datastores Script

-- DataStore

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetOrderedDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local noobCoins = Instance.new("IntValue")
	noobCoins.Name = "Noob Coins"
	noobCoins.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.. "-noobCoins")
	end)
	
	if success then
		noobCoins.Value = data
	else
		print("There was an error whilest loading your data!")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-noobCoins",player.leaderstats.Cash.Value)
	end)
	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error while saving data!")
		warn(errormessage)
	end
	
end)

CollectScript

WaitTime = 60 -- How many seconds to respawn the coin!
Amount = 1 -- Amount of coins per coin!
function onTouched(part)
	local h = part.Parent:findFirstChild("Humanoid")
	if (h~=nil) then
		local thisplr = game.Players:FindFirstChild(h.Parent.Name)
		if (thisplr~=nil) then
			local stats = thisplr:findFirstChild("leaderstats")
			if (stats~=nil) then
				local Score = stats:findFirstChild("Noob Coins")
				if (Score~=nil) then
					Score.Value = Score.Value + Amount
				end
			end
		end
		script.Parent.Transparency = 1
		script.Parent.CanCollide = false
		script.Parent.DecalBack.Transparency = 1
		script.Parent.DecalFront.Transparency = 1
		script.Disabled = true
		wait(WaitTime)
		script.Parent.Transparency = 0
		script.Parent.CanCollide = true
		script.Parent.DecalBack.Transparency = 0
		script.Parent.DecalFront.Transparency = 0
		script.Disabled = false
	end
end

script.Parent.Touched:Connect(onTouched)

leaderstats has no child called Cash, it should be like this:

myDataStore:SetAsync(player.UserId.."-noobCoins", player.leaderstats:FindFirstChild("Noob Coins").Value)
1 Like