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

how kick people with leaderstats?

Asked by 4 years ago

okey so what i want to do is kick people when they die 5 times or more, i already tried adding things to my leaderboard script but nothing happens (when i die 5 times i dont get kick)



local Counter = "Deaths" game.Players.PlayerAdded:connect(function(Player) local leaderstats = Player:FindFirstChild('leaderstats') or Instance.new("Folder", Player) leaderstats.Name = "leaderstats" local deaths = leaderstats:FindFirstChild(Counter) or Instance.new("IntValue", leaderstats) deaths.Name = Counter deaths.Value = 0 if deaths.Value >= 5 then -- here nothing happens Player:Kick("You have died too many times!") end Player.CharacterAdded:connect(function(Character) local d = true Character:WaitForChild("Humanoid").Died:connect(function() if (d) then d = false deaths.Value = deaths.Value + 1 end end) end) end)

thats my script

0
It's not working, because you're not repeatedly checking the value of the deaths. This is just running once and stops. The CharacterAdded event will run everytime the player respawns/dies. killerbrenden 1537 — 4y

4 answers

Log in to vote
0
Answered by 4 years ago

You could use coroutine, if you know what that is. Or you could just do a while loop.

local Counter = "Deaths"

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local deaths = Instance.new("IntValue")
    deaths.Name = Counter
    deaths.Value = 0
    deaths.Parent = leaderstats

    while wait() do
        if deaths.Value >= 5 then
            player:Kick("You Have Died Too Many Times!")
        end
    end
end)

This is the simple way, a while loop.

Here is the coroutine way.

local Counter = "Deaths"

local checkDeaths = coroutine.wrap(function(player,deaths)
    while wait() do
        if deaths.Value >= 5 then
            player:Kick("You Have Died Too Many Times!")
        end
    end
end)

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local deaths = Instance.new("IntValue")
    deaths.Name = Counter
    deaths.Value = 0
    deaths.Parent = leaderstats

    checkDeaths(player,deaths)
end)

Either way works! Hope this helped!

Ad
Log in to vote
0
Answered by 4 years ago
local player = game.Players.LocalPlayer
local char = player.CharacterAdded:Wait()
local stats = player.leaderstats
local deaths = stats.Deaths
local remote = workspace.Script.RemoteEvent

if deaths.Value == 5 then
    player:Kick("You have died too many times!")
end

this is a local script, put it in starter pack, it should fix your issue.

Log in to vote
0
Answered by 4 years ago

This would work if not for the fact you're only checking the 'deaths' value once. You could either put a loop of some sort in to continually check the value, but that's not very efficient. It would be better to put the check right after the player dies and you increment the value.

First ditch the original check if statement on line ten.

Now change the death code instead of this:

local d = true
Character:WaitForChild("Humanoid").Died:connect(function()
    if (d) then
        d = false
        deaths.Value = deaths.Value + 1
    end
 end)

It could be this:

local d = true
Character:WaitForChild("Humanoid").Died:connect(function()
    if (d) then
        d = false
        deaths.Value = deaths.Value + 1
    if (deaths.Value >= 5) then --//Checking right after the player dies
        Player:Kick("You have died too many times!")
    end
    end
 end)

Far more efficient.

Log in to vote
0
Answered by 4 years ago

this works, I tested it

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder",plr)
    leaderstats.Name = "leaderstats"

    local deaths = Instance.new("IntValue",leaderstats)
    deaths.Name = "Deaths"
    deaths.Value = 0

    plr.CharacterAdded:Connect(function(char)
        char:WaitForChild("Humanoid").Died:connect(function()
            deaths.Value = deaths.Value + 1
        end)
        while true do
            wait()
            if deaths.Value >= 5 then
                plr:Kick("You have died too many times, "..plr.Name)
            end
        end
    end)
end)

Answer this question