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

Help with data being taken away?

playerdata = {}
local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerData")

local maxlevel = 50

game.Players.PlayerAdded:connect(function(player)

	mydata = datastore:GetAsync(player.userId) or {}

	mydata.level = mydata.level or 1
	mydata.xp = mydata.xp or 0
	mydata.wins = mydata.wins or 0
	
	playerdata[player.userId] = mydata		

	local leaderstats = player:FindFirstChild("leaderstats")
	local newLeaderstats = Instance.new("Model", player)
	newLeaderstats.Name = "leaderstats"

	local Level = Instance.new("IntValue", newLeaderstats)
	Level.Name = "Level"
	Level.Value = mydata.level
	
	local Wins = Instance.new("IntValue", player)
	Wins.Name = "Wins"
	Wins.Value = mydata.wins
	
	local XP = Instance.new("IntValue", player)
	XP.Name = "XP"
	XP.Value = mydata.xp
			
	while wait() do
		local extralevel = Level.Value
		local xpneeded = ((extralevel * 2) - 1) * 40
		if Level.Value < maxlevel then
			if XP.Value >= xpneeded then
				Level.Value = mydata.level + 1
				mydata.level = Level.Value
				XP.Value = mydata.xp - xpneeded
				mydata.xp = XP.Value
			end
		end	
	end
end)

On the odd occasion, when a player levels up, the XP seems to go to negative, for example, if you are Lv 2, you need 120 XP to level up to Lv.3, and if the game gave you 130XP, it would level you up to Lv.3 and you would keep 10XP, but instead of it taking 120 XP from the player, it seems to take 120XP, but make their leftover XP to negative.

Their are no errors in the output, and it looks perfectly fine to me.

This error only occurs occasionally.

1 comment

0

You should be using a changed event, not an infinite loop.

1 answer

firstly instead of using mydata.level = mydata.level or 1 you should do

if type(mydata.level)  == 'number' then -- this checks to see if mydata.level is a number
mydata.level = math.floor(mydata.level, 1)--[[as this will take the highest value, so now there will no negative levels, just a little saftey precaution--]]
else mydata.level = 1
end
-- sure a bit more work but worth it in the end

another thing you are doing wrong is this ...

 XP.Value = mydata.xp - xpneeded

you are presuming that xpneeded is always larger than mydata.xp but since mydata.xp can be 0 you can presume that xpneeded will be larger in most cases. plus by doing

 XP.Value = mydata.xp - xpneeded
mydata.xp = XP.Value

your not getting the xp the player should be getting. instead do ...

if Level.Value < maxlevel and mydata.xp - xpneeded>=0 then --[[ makes sure it wont get a -value--]]
            if XP.Value >= xpneeded then
                Level.Value = mydata.level + 1
                mydata.level = Level.Value
                XP.Value = mydata.xp - xpneeded
                mydata.xp = XP.Value
            end
        end

lastly I'll say that your not saving any data which will cause none of the data you got to save. So In the end just do ...

playerdata = {}
local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerData")

local maxlevel = 50

game.Players.PlayerAdded:connect(function(player)

    mydata = datastore:GetAsync(player.userId) or {}

    if type(mydata.level)  == 'number' then -- this checks to see if mydata.level is a number
mydata.level = math.floor(mydata.level, 1)--[[as this will take the highest value, so now there will no negative levels, just a little saftey precaution--]]
else mydata.level = 1
end
-- sure a bit more work but worth it in the end
   if type(mydata.xp)  == 'number' then -- this checks to see if mydata.xp is a number
mydata.level = math.floor(mydata.xp, 0)--[[as this will take the highest value, so now there will no negative levels, just a little saftey precaution--]]
else mydata.level = 0
end
-- sure a bit more work but worth it in the end
   if type(mydata.wins)  == 'number' then -- this checks to see if mydata.winsis a number
mydata.wins = math.floor(mydata.wins, 0)--[[as this will take the highest value, so now there will no negative levels, just a little saftey precaution--]]
else mydata.wins =  0

end

    playerdata[player.userId] = mydata      

    local leaderstats = player:FindFirstChild("leaderstats")
    local newLeaderstats = Instance.new("Model", player)
    newLeaderstats.Name = "leaderstats"

    local Level = Instance.new("IntValue", newLeaderstats)
    Level.Name = "Level"
    Level.Value = mydata.level

    local Wins = Instance.new("IntValue", player)
    Wins.Name = "Wins"
    Wins.Value = mydata.wins

    local XP = Instance.new("IntValue", player)
    XP.Name = "XP"
    XP.Value = mydata.xp

    while wait() do
        local extralevel = Level.Value
        local xpneeded = ((extralevel * 2) - 1) * 40
 if Level.Value < maxlevel and mydata.xp - xpneeded>=0 then --[[ makes sure it wont get a -value--]]
            if XP.Value >= xpneeded then
                Level.Value = mydata.level + 1
                mydata.level = Level.Value
                XP.Value = mydata.xp - xpneeded
                mydata.xp = XP.Value
            end
        end
    end
end)

Lastly, I would appreciate if you gave me some upvotes. I am really down right now because of some random people, so I would really appreciate it.

3 comments

0

If you read my question properly, you would see that I did always have mydata.xp >= xpneeded

0

no ... you didnt

0

stop hating on me. what I gave you more, less fixes your problems because I bet that there i another script or more script that is effecting that part of your script. well no more.

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