Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does my Level up system not level up the player when the xp is higher than the xp needed?

So i have made this script which is a Level up system integrated with the datastore system, now the datastore works well but the Level up part Line 26 - 32 does not. What is the issue? and how do i fix it?

the code:

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("Level")
local ds2 = DataStore:GetDataStore("XP")
local ds3 = DataStore:GetDataStore("XPN")
local success, err = pcall(function()

game.Players.PlayerAdded:connect(function(player)
	local leader = Instance.new("Folder", player)
	leader.Name = "leaderstats"
	------------------------------------------
	local level = Instance.new("IntValue", leader)
	level.Name = "Level"
	level.Value = ds1:GetAsync(player.UserId) or 1
	ds1:SetAsync(player.UserId.."-Level", level.Value)
	------------------------------------------
	local xp = Instance.new("NumberValue", player)
	xp.Name = "XP"
	xp.Value = ds2:GetAsync(player.UserId) or 1
	ds2:SetAsync(player.UserId.."-XP", xp.Value)
	------------------------------------------
	local XPN = Instance.new("IntValue", player)
	XPN.Name = "XPNeeded"
	XPN.Value = ds3:GetAsync(player.UserId) or 1000
	ds2:SetAsync(player.UserId.."-XPNeeded", XPN.Value)
	
	 	xp.Changed:connect(function()
		print("+XP SAVING")
  		if player:WaitForChild("XP").Value >= player:WaitForChild("XpNeeded").Value then
  			level.Value = level.Value+1
			XPN.Value = XPN.Value + 500
    		XPN.Value = XPN.Value
			xp.Value = 0
		ds1:SetAsync(player.UserId.."-Level", level.Value)
		ds2:SetAsync(player.UserId.."-XP", xp.Value)
		ds3:SetAsync(player.UserId.."-XPNeeded", XPN.Value)
			else
			ds1:SetAsync(player.UserId.."-Level", level.Value)
			ds2:SetAsync(player.UserId.."-XP", xp.Value)
			ds3:SetAsync(player.UserId.."-XPNeeded", XPN.Value)
		end


game.Players.PlayerRemoving:connect(function(player)
	
	local SavedData = Instance.new("BindableEvent")
	ds1:setAsync(player.UserId, player.leaderstats.Level.Value)--Level
	ds2:setAsync(player.UserId, player.xp.Value)--XP
	ds2:setAsync(player.UserId, player.XPNeeded.Value)--XPNeeded
	print("SavingData")
	
	SavedData:Fire()

game:BindToClose(function()
    SavedData.Event:Wait()
end)
end)
end)
end)
end)	

2 comments

0

What is the output?

0

Output is clear

1 answer

I guess i solved it guys i had to edit the level up part to this :

xp.Changed:connect(function()
		print("+XP SAVING")
  		if xp.Value >= XPN.Value then	
  		player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
			XPN.Value = XPN.Value + 500
    		XPN.Value = XPN.Value
			xp.Value = 0
		ds1:SetAsync(player.UserId.."-Level", level.Value)
		ds2:SetAsync(player.UserId.."-XP", xp.Value)
		ds3:SetAsync(player.UserId.."-XPNeeded", XPN.Value)
			else
			ds1:SetAsync(player.UserId.."-Level", level.Value)
			ds2:SetAsync(player.UserId.."-XP", xp.Value)
			ds3:SetAsync(player.UserId.."-XPNeeded", XPN.Value)
		end

1 comment

0

I solved the issue but now the problem is that the XP and XPNeeded dont save :(

This discussion is preserved as it was. New answers, comments, and votes are closed.