Hello, I am making a leaderboard that DataStores all the information and is updated when the players data is updated. Everything works fine, except for one issue. In lines 37 & 38, the script tells each certain leaderstat to give a certain amount of points to anybody who is an admin. The script works fine, but the problem is lvl.Value = lvl.Value + 1
is not firing. Instead, lvl.Value
thinks that it is p
, so therefore the admin levels go up 100 instead of 1. I could not figure out the issue with it, so hopefully somebody can.
Code:
local ds = game:GetService("DataStoreService"):GetDataStore("Points") local lvls = game:GetService("DataStoreService"):GetDataStore("Level") Admins = {"Player", "turtletowerz", "T3XLAlt", "Natenewt"} --add names function onEntered(player) local stats = Instance.new("IntValue") stats.Name = "leaderstats" local p = Instance.new("IntValue", stats) p.Name = "Points" local lvl = Instance.new("NumberValue", stats) lvl.Name = "Level" stats.Parent = player if ds:GetAsync("Key" .. player.Name) then --Checks if the player has a save p.Value = ds:GetAsync("Key" .. player.Name) --Set the cash value to the save else p.Value = 10 --If no save they start with 100. Change to a different value if you want end p.Changed:connect(function(save) --When the cash changes ds:SetAsync("Key" .. player.Name, save)--Save end) if lvls:GetAsync("Key" .. player.Name) then lvl.Value = ds:GetAsync("Key" .. player.Name) else lvl.Value = 1 end p.Changed:connect(function(save) lvls:SetAsync("Key" .. player.Name, save) end) for i=1,#Admins do if player.Name == Admins[i] then wait(0.1) p.Value = p.Value + 100 --THIS IS NOT THE PROBLEM, THIS IS THE POINT VALUE lvl.Value = lvl.Value + 1 --HERE IS THE PROBLEM. THIS WILL NOT WORK end end
I looked into your script, added the missing end and I found you weren't checking lower case names. Your name in the script was turtletowerz and because it's case sensitive Turtletowerz was not recognised. This was a simple fix by just converting the player's name and the name in the list to lower case whilst comparing them (line 35) by using string.lower(string). Also, if a player has changed their name the datastore wont pick up their data, you should use "Key" .. tostring(player.UserId) not "Key" .. player.Name.
local ds = game:GetService("DataStoreService"):GetDataStore("Points") local lvls = game:GetService("DataStoreService"):GetDataStore("Level") Admins = {"Player", "turtletowerz", "T3XLAlt", "Natenewt","UndeniableLimited"} --add names function onEntered(player) local stats = Instance.new("IntValue") stats.Name = "leaderstats" local p = Instance.new("IntValue", stats) p.Name = "Points" local lvl = Instance.new("NumberValue", stats) lvl.Name = "Level" stats.Parent = player if ds:GetAsync("Key" .. tostring(player.UserId)) then --Checks if the player has a save p.Value = ds:GetAsync("Key" .. tostring(player.UserId)) --Set the cash value to the save else p.Value = 10 --If no save they start with 100. Change to a different value if you want end p.Changed:connect(function(save) --When the cash changes ds:SetAsync("Key" .. tostring(player.UserId), save)--Save end) if lvls:GetAsync("Key" .. tostring(player.UserId)) then lvl.Value = ds:GetAsync("Key" .. tostring(player.UserId)) else lvl.Value = 1 end p.Changed:connect(function(save) ds:SetAsync("Key" .. tostring(player.UserId), save) -- You put lvls:SetAsync not ds:SetAsync end) for i=1,#Admins do if string.lower(player.Name) == string.lower(Admins[i]) then -- Changed wait(0.1) p.Value = p.Value + 100 lvl.Value = lvl.Value + 1 break -- Get out of this loop, the player's been found! end end end
Hope this works for you and is what you wanted, please vote up and accept as answer if it does!
NOTE: The reason you could be lvl 100 when you join is because your data will be saved, however with this new script all data will also be lost so your lvl 101 will be reset but will go up by one each time you join!