local DataStoreService = game:GetService("DataStoreService")
local PDS = DataStoreService:GetDataStore("PointSim1235")


game.Players.PlayerAdded:Connect(function(NewPlayer)
	
	local Key = "PDS-" .. NewPlayer.UserId
	
	local GetSave = PDS:GetAsync(Key)
	
	local leaderstats = Instance.new("Folder", NewPlayer)
	leaderstats.Name = "leaderstats"
	
	local points = Instance.new("IntValue", leaderstats)
	points.Name = "Points"
	
	local Rebirths = Instance.new("IntValue", leaderstats)
	Rebirths.Name = "Rebirths"
	Rebirths.Value = 1
	
	local PlayerStats = Instance.new("Folder", NewPlayer)
	PlayerStats.Name = "Playerstats"
	
	local ClickBonus = Instance.new("IntValue", PlayerStats)
	ClickBonus.Name = "ClickBonus"
	
	NewPlayer.CharacterAdded:Connect(function()
		local character = NewPlayer.Character
		local At1 = Instance.new("Attachment", character:WaitForChild("Head"))
        local At2 = Instance.new("Attachment", character:WaitForChild("HumanoidRootPart"))
        At1.Name = "At1"
		At2.Name = "At2"
	end)
	
	if GetSave then --load data
		points.Value = GetSave[1]
		Rebirths.Value = GetSave[2]
		ClickBonus.Value = GetSave[3]
		print("Data Loaded For " .. NewPlayer.Name)
	else
		points.Value = GetSave[0]
		Rebirths.Value = GetSave[1]
		ClickBonus.Value = GetSave[0]
		print("New Data Created For " .. NewPlayer.Name)
	end	
end)


game.Players.PlayerRemoving:Connect(function(player)
	print("Saving Data For " .. player.Name)
	
	local Key = "PDS-" .. NewPlayer.UserId
	
	local Stats1 = player.leaderstats
	local Stats2 = player.Playerstats
	
	local DataToSave = {}		
	for _, Stat in pairs(Stats1:GetChildren()) do
		table.insert(DataToSave, Stat.Value)
		warn("Player Left, Saving " .. Stat.Name .. " | " .. Stat.Value)
	end
	for _, Stat in pairs(Stats2:GetChildren()) do
		table.insert(DataToSave, Stat.Value)
		warn("Player Left, Saving " .. Stat.Name .. " | " .. Stat.Value)
	end
	
	PDS:SetAsync(Key, DataToSave)
end)

6 comments

0

On line 50 you are using "NewPlayer", which can only be accessed in the scope of the Players.PlayerAdded event, use "player" instead

0

wait wut? can u explain that more clearly pls?

0

ok thx it worked, but wut about line 41?

0

There can't be a space when accessing a table value.

0

huh?

0

im a noob at scripting so i don't really understand, srry for that