local DataStore = game:GetService("DataStoreService"):GetDataStore("Everything")
local saveRemote = game:GetService("ReplicatedStorage"):WaitForChild("Save")

game.Players.PlayerAdded:Connect(function(Plr)
	local stats = Instance.new("Folder", Plr)
	stats.Name = "Data"
	--- Level System
	local Levels = Instance.new("IntValue", stats)
	Levels.Name = "Levels"
	Levels.Value = 1
	local Exp = Instance.new("IntValue", stats)
	Exp.Name = "Exp"
	Exp.Value = 0
	local hair = Instance.new("StringValue", stats)
	hair.Name = "Hair"
	local outfit = Instance.new("StringValue", stats)
	outfit.Name = "Outfit"
	local accessory = Instance.new("StringValue", stats)
	accessory.Name = "Accessory"
	
	local success, errormsg = pcall(function()
		local getData = DataStore:GetAsync(Plr.UserId)
		for i, v in pairs(getData) do
			   if i == 1 then
				Levels.Value = getData[i]
			elseif i == 2 then
				Exp.Value = getData[i]
			elseif i == 3 then
				ExpNeed.Value = getData[i]
			elseif i == 4 then
				Gold.Value = getData[i]
			elseif i == 5 then
				DefenseP.Value = getData[i]
			elseif i == 6 then
				SwordP.Value = getData[i]
			elseif i == 7 then
				LuckP.Value = getData[i]		
			end
		end
	end)
	

if success then
		print("Successfully loaded "..Plr.Name.."'s data!")
	elseif not success then
		-- FIRST TIME JOINING --
		Levels.Value = 0
		Exp.Value = 0
		ExpNeed.Value = 100
		Gold.Value = 10
		DefenseP.Value = 0
		SwordP.Value = 0
		LuckP.Value = 0
		Special.Value = 0
		hair.Value = "No Hair Hair"
		outfit.Value = "No Outfit Outfit"
		accessory.Value = "Shades"
		error("Something went wrong while loading the data of "..Plr.Name..": "..errormsg)
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	wait(.1)
	local Exp = plr.Data.Exp
	local Levels = plr.Data.Levels
	local ExpNeed = plr.Data.ExpNeed
	local Points = plr.Data.Points
	
	while wait() do
		if Exp.Value >= (100 * (Levels.Value + 1)) and Levels.Value <= 399 then
			Levels.Value = Levels.Value + 1
			Points.Value = Points.Value + 3
			Exp.Value = Exp.Value - ExpNeed.Value
			ExpNeed.Value = ExpNeed.Value + 100
			game.ReplicatedStorage.LevelSystem.LevelUpGui:FireClient(plr)
		end
	end
end)

saveRemote.OnServerEvent:Connect(function(player, val)
	DataStore:SetAsync(player.UserId, val)
	print("Successfully saved "..player.Name.."'s data!")
end)

so this is it and i legit dont know what wrong

2 comments

0

Can you simplify the code

0

i tried to simplify it a lil by removing repeatative lines how is it now?