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

How do you use an already existing leaderstat and not create a new one? (READ EDIT)

Asked by
D00M1N 4
2 years ago
Edited 2 years ago

Most youtube tutorials on making stuff related to leaderstats, like a shop have them making a new leaderstat. The tutorial I followed was about making a trail shop, and the currency points. When you kill a player you are awarded a kill on the leaderstats and about 15 points. I want to use the leaderstat "Points," which is was created in the script where when you are awarded a kill and points when you kill someone (which I'll call Kill counter below). How do I not make another leaderstat called "Points?"

edit: Also if I create another "Points" leaderstat, it won't appear or it will appear but the "Kills" won't

edit2: Scrap the first edit, I just realized it was not a leaderstat. I will be making changes. For now it is solved.

Code for the "Kill counter"

local Players = game.Players

local Template = Instance.new 'BoolValue'
Template.Name = 'leaderstats'

Instance.new('IntValue', Template).Name = "Kills"
Instance.new ('IntValue', Template).Name = "Points"

Players.PlayerAdded:connect(function(Player)
    wait(1)
    local Stats = Template:Clone()
    Stats.Parent = Player
    Player.CharacterAdded:connect(function(Character)
        local Humanoid = Character:FindFirstChild "Humanoid"
        if Humanoid then
            Humanoid.Died:connect(function()
                for i, Child in pairs(Humanoid:GetChildren()) do
                    if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
                        local Killer = Child.Value
                        if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
                            local Kills = Killer.leaderstats.Kills
                            local Points = Killer.leaderstats.Points
                            Kills.Value = Kills.Value + 1
                            Points.Value = Points.Value + 15
                        end
                        return
                    end
                end
            end)
        end
    end)
end)

And for the script where I need to use the already made leaderstat.

Line 28 is the former problem.

local dataStore = game:GetService("DataStoreService"):GetDataStore("TrailShopTest")


local function EquipTrail(plr,trail)
    local char = plr.Character

    if trail ~= nil and char ~= nil then

        if char.HumanoidRootPart:FindFirstChild("Attachment") then char.HumanoidRootPart.Attachment:Destroy() end
        if char.Head:FindFirstChild("Attachment") then char.Head.Attachment:Destroy() end

        if char:FindFirstChild(plr.Name.."'s Trail") then char[plr.Name.."'s Trail"]:Destroy() end
        trail.Name = plr.Name.."'s Trail"

        local attachment1 = Instance.new("Attachment",char:FindFirstChild("HumanoidRootPart"))
        local attachment2 = Instance.new("Attachment",char:FindFirstChild("Head"))

        trail.Attachment0 = attachment1
        trail.Attachment1 = attachment2

        trail.Parent = char
    end
end

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


    local Points = Instance.new("IntValue",player); Points.Name = "Points" -- ****former problem
    local trailInventory = Instance.new("Folder",player); trailInventory.Name = "TrailInventory"
    local equippedTrail = Instance.new("StringValue",player); equippedTrail.Name = "EquippedTrail"


    player.CharacterAdded:Connect(function(char)

        if game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value) then
            EquipTrail(player,game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value):Clone())
        end
    end)

    equippedTrail.Changed:Connect(function()
        if equippedTrail.Value ~= nil then
            if game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value) then
                EquipTrail(player,game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value):Clone())
            end
        end
        if equippedTrail.Value == "" then

            local char = player.Character

            local root = char:FindFirstChild("HumanoidRootPart")
            local head = char:FindFirstChild("Head")

            if root:FindFirstChild("Attachment") then root.Attachment:Destroy() end
            if head:FindFirstChild("Attachment") then head.Attachment:Destroy() end

            if char:FindFirstChild(player.Name.."'s Trail") then char[player.Name.."'s Trail"]:Destroy() end
        end
    end)


    local data

    local success, errorMsg = pcall(function()
        data = dataStore:GetAsync(player.UserId)
    end)

    if data ~= nil then
        if data.Points then
            Points.Value = data.Points
        end
        if data.EquippedTrail then
            equippedTrail.Value = data.EquippedTrail
        end
        if data.Trails then
            for index,trail in pairs(data.Trails) do
                local bool = Instance.new("BoolValue",trailInventory); bool.Name = trail
            end
        end
    else
        print("Someone has joined the game")
    end

    game.ReplicatedStorage.Events.SendData:FireClient(player,data)
end)

game.Players.PlayerRemoving:Connect(function(player)
    local data = {}

    data.Points = player.Points.Value

    data.EquippedTrail = player.EquippedTrail.Value

    data.Trails = {}

    for i,v in pairs(player.TrailInventory:GetChildren()) do
        table.insert(data.Trails,v.Name)
    end

    local success, errorMsg = pcall(function()
        dataStore:SetAsync(player.UserId,data)
    end)

    if success then 
        print("Succesfully saved") 
    else 
        print(errorMsg) 
    end

end)

game:BindToClose(function()
    for index, player in pairs(game.Players:GetPlayers()) do
        local data = {}

        data.Points = player.Points.Value

        data.EquippedTrail = player.EquippedTrail.Value

        data.Trails = {}

        for i,v in pairs(player.TrailInventory:GetChildren()) do
            table.insert(data.Trails,v.Name)
        end

        local success, errorMsg = pcall(function()
            dataStore:SetAsync(player.UserId,data)
        end)

        if success then 
            print("Succesfully saved") 
        else 
            print(errorMsg) 
        end
    end
end)

game.ReplicatedStorage.Events.BuyItem.OnServerInvoke = function(player, trailName)

    local trail = game.ReplicatedStorage.Trails:FindFirstChild(trailName)
    local ifsInInventory

    if player.TrailInventory:FindFirstChild(trailName) then
        ifsInInventory = true
    end

    if trail then
        if trail:FindFirstChild("Price") then
            if not ifsInInventory then
                if player.Points.Value >= trail.Price.Value then
                    print(player.Name.." brought the ".. trail.Name.." trail")

                    player.Points.Value = player.Points.Value - trail.Price.Value

                    local bool = Instance.new("BoolValue",player.TrailInventory); bool.Name = trail.Name

                    return "Brought"
                else
                    return "NotEnough"
                end
            else
                if player.EquippedTrail.Value ~= trail.Name then
                    player.EquippedTrail.Value = trail.Name
                    print(player.Name.." equipped the ".. trail.Name.." trail")
                    return "Equip"
                else
                    player.EquippedTrail.Value = ""
                    print(player.Name.." unequipped the ".. trail.Name.." trail")
                    return "Unequip"
                end
            end
        end
    end

end

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

A bit confused with what you're asking but you could just wait for the first one to be created from your first script if thats what you're looking for.

--instead of
local Points = Instance.new("IntValue",player); Points.Name = "Points"
--use
local Points = player:WaitForChild('leaderstats'):WaitForChild("Points")
0
It says: Points is not a valid member of Player "Players.D00M1N" D00M1N 4 — 2y
0
Sorry if I responded again late, but I just realized it was not a leaderstat. I am a bit dumb and sorry if I wasted a bit of your time. D00M1N 4 — 2y
Ad

Answer this question