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

Accessing data in leaderstats??

Asked by 4 years ago

Hi i am trying to access data that is being stored in leaderstats by another script.

I have a script that writes Exp as a number value to the leaderstats and that is working ok.

I am then trying to access that number to obtain which rank to assign a play.

it seems to access the leaderstat data but with the value as 2 it should assign my rank as private but it is assigning me corporal for some reason.

It is most likely something simple but i am new to Lua and have pulled information from several scripts to write this.

Any ideas??

local Players = game:GetService("Players")
local ExpDataStore = game:GetService("DataStoreService"):GetDataStore("Exp") 
local RankGui = game:GetService("ServerStorage"):WaitForChild("RankGui")


local function onPlayerAdded(player)
    local playerKey = "Player_" .. player.UserId
    local leaderstats = player:WaitForChild('leaderstats')
    leaderstats.Name = "leaderstats"

    local myExp
    local success, err = pcall(function()
        myExp = ExpDataStore:GetAsync(playerKey)
    end)
    if success then
        print ("Experience Data Found")

        --Private
        if myExp >= 1 then -- Set the number to your group ID !
            print("Private")        
            local clonedgui = RankGui:Clone()
            clonedgui.TextLabel.Text = "Private"
            clonedgui.TextLabel.TextColor3 = Color3.fromRGB(36,154,136)
            --clonedgui.Parent = character.Head
            clonedgui.Parent = game.Workspace:WaitForChild(player.Name).Head -- Yes, you can also just say character.Head                       
        end 
        --Lance Corporal
        if myExp >= 50 then -- Set the number to your group ID !
            local clonedgui = player.Character.Head:FindFirstChild("RankGui")
            print("Lance Corporal")     
            --local clonedgui = RankGui:Clone()
            clonedgui.TextLabel.Text = "Lance Corporal"
        end
        --Corporal
        if myExp >= 150 then -- Set the number to your group ID !
            local clonedgui = player.Character.Head:FindFirstChild("RankGui")
            print("Corporal")       
            --local clonedgui = RankGui:Clone()
            clonedgui.TextLabel.Text = "Corporal"
        end
        --Sergeant
        if myExp >= 300 then -- Set the number to your group ID !
            local clonedgui = player.Character.Head:FindFirstChild("RankGui")
            print("Sergeant")       
            --local clonedgui = RankGui:Clone()
            clonedgui.TextLabel.Text = "Sergeant"
        end
        --Staff Sergeant
        if myExp >= 500 then -- Set the number to your group ID !
            local clonedgui = player.Character.Head:FindFirstChild("RankGui")
            print("Staff Sergeant")     
            --local clonedgui = RankGui:Clone()
            clonedgui.TextLabel.Text = "Staff Sergeant"
        end
        --Warrant Officer Class 2
        if myExp >= 1000 then -- Set the number to your group ID !
            local clonedgui = player.Character.Head:FindFirstChild("RankGui")
            print("Warrant Officer Class 2")        
            --local clonedgui = RankGui:Clone()
            clonedgui.TextLabel.Text = "WO2"
        end
        --Warrant Officer Class 1
        if myExp >= 2500 then -- Set the number to your group ID !
            local clonedgui = player.Character.Head:FindFirstChild("RankGui")
            print("Warrant Officer Class 1")        
            --local clonedgui = RankGui:Clone()
            clonedgui.TextLabel.Text = "WO1"
        end

    else
        -- Failed to retrieve data
    end

    leaderstats.Parent = player
end

for _, player in pairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

Answer this question