Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Separate leaderstats not adding correctly?

Asked by 8 years ago

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
0
You're missing an end, I'm assuming that's because you didn't copy the whole script... General_Scripter 425 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

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!

0
string.lower is unnecessary and has a security flaw - if someone names himself turtletowerz, t3xlalt or natenewt lowercase he can get admin. Just compare player name with admin name without affecting the strings. Marios2 360 — 8y
0
It doesn't have a security flaw, you can't take someone else's name by just changing the case of one or more letters, you have to add or change something or it won't let you take it. General_Scripter 425 — 8y
0
I don't think you quite understand my issue. The problem is that it adds on 101 points, and 101 levels as if levels and points were the same Value. User#9949 0 — 8y
0
Sorry, I didn't realise, but have now found the problem! Please read what my answer (make sure you take note of the note at the bottom). General_Scripter 425 — 8y
Ad

Answer this question