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

How do I get a code to run once leader-board hits 30 units?

Asked by 4 years ago
Edited 4 years ago

So I am making a Roblox 3rd Person shooter game and I have a leaderboard script in ServerScriptStorage which has this code

local Players = game.Players

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

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


Players.PlayerAdded:connect(function(Player)
    wait(1)
    local Stats = Template:Clone()
    Stats.Parent = Player
    local Deaths = Stats.Deaths
    Player.CharacterAdded:connect(function(Character)
        Deaths.Value = Deaths.Value + 1
        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
                            Kills.Value = Kills.Value + 1
                            end
                        end
                        return -- Only one player can get a KO for killing a player. Not 2, not 3. Only one.
                    end
                end
            end)
        end
    end)
end)

So at the very end I am trying to get if kills = 30 then run a code.. here's my attempt

if kills == 30 then
--Runs a code for example
print("Worked")

but it didn't work and I am not a roblox engineer I am a newbie at scripting so don't just give me a code and expect me to understand it

0
you need some kind of loop that constantly checks to see if kills == 1 DarkDanny04 407 — 4y

1 answer

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

This will be needed to be somewhere inside the player added function, and after the point that you define Kills as a variable

Kills.Changed:Connect(function(property)
    if property == "Value" then
        if Kills.Value == 30 then
            --Code
            print("Worked")
        end
    end
end)

What this does is it detects every time the value of Kills changes. You could use a while true do loop, but with this way, it doesnt always run which reduces server lag. If it ever changes and equals 30, then something will happen.

The reason you can't just have if statements on their own is that they only check if that statement is true when the server starts up, so they must be encased in some type of repeatable function or a loop.

I hope this helps, and if it did please accept the answer :)

Ad

Answer this question