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

How would I make a convenient Idle player kill script?

Asked by 9 years ago

Last year I asked how to make it so It prints when the player isn't jumping or walking so I used Idle event like this

game.Players.Player.Idled:connect(function(time)
     print("Player has been idle for " .. time .. " seconds")
end)

I just wanted to make a script that kills a player if they stay still for 5 seconds. Here is my attempt.

IdleTime = 5

game.Players.PlayerAdded:connect(function(plyr)
    plyr.CharacterAdded:connect(function(char)
        char.Humanoid.Running:connect(function()
        wait(IdleTime)
            char.Humanoid.Running:connect(function(speed)
                if speed == 0 then
                char.Humanoid.Health = 0
                end
            end)
        end)
    end)
end)

It works but it's a little glitchy, like if they stay still then move for a little then stay still they immediately die.


Answer

player = game.Players.LocalPlayer
repeat wait(0) until player.Character
character = player.Character
humanoid = character:WaitForChild("Humanoid")

MaxIdleTime = 5
CurrentIdleTime = MaxIdleTime

humanoid.Running:connect(function()
    CurrentIdleTime = MaxIdleTime
end)

repeat
    CurrentIdleTime = CurrentIdleTime - 1
wait(1)
until CurrentIdleTime <= 1
humanoid.Health = 0

2 answers

Log in to vote
0
Answered by
Discern 1007 Moderation Voter
9 years ago

I sort of remade your script. Instead of one script watching every other player, I find it easier for each person to have their own script.

In order to do this, you would put a LocalScript inside StarterGui or StarterPack.

This script basically counts down a timer every second, and if the timer hits 0, then it kills the player. Whenever the player moves, it sets the timer back to the maximum amount of time you're allowed to be idle.

Script has been tested and works.

player = game.Players.LocalPlayer
repeat wait() until player.Character
character = player.Character
humanoid = character:WaitForChild("Humanoid")

MaxIdleTime = 5 --This is the amount of seconds that someone can be idle for.
CurrentIdleTime = MaxIdleTime

humanoid.Running:connect(function()
    CurrentIdleTime = MaxIdleTime
end)

humanoid.Jumping:connect(function()
    CurrentIdleTime = MaxIdleTime
end)

while true do
    CurrentIdleTime = CurrentIdleTime - 1
    if CurrentIdleTime == 0 then
        character:BreakJoints()
    end
    wait(1)
end

NOTE: If you are using FilteringEnabled, you have to connect a LocalScript to a server script through a RemoteEvent to kill the player. I do not know how to use this that well, so I can't really give you an answer if you are using FilteringEnabled. The above script only works if FilteringEnabled is disabled.

If I helped you, accept my answer please! :D

0
I edited your script and made it better since the way you did it it would actually take 6 seconds instead of 5. I fixed it and removed some parts of it. EzraNehemiah_TF2 3552 — 9y
0
It still glitches a little though. EzraNehemiah_TF2 3552 — 9y
0
Well this script makes it so if you keep moving nonstop for 5 seconds, then it would kill you. Is that considered the "glitch"? Discern 1007 — 9y
0
Yep EzraNehemiah_TF2 3552 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

You could also use the Velocity property of a part to see if it is moving.

game.Players.PlayerAdded:connect(function(Player)
    Player.CharacterAdded:connect(function(Character)
        local IdleCount = 0
        while wait(1) do
            if Player.Character.Torso.Velocity == Vector3.new(0,0,0) then
                IdleCount = IdleCount + 1
            else
                IdleCount = 0
            end 
            if IdleCount >= 5 then
                Player.Character:BreakJoints()
            end
        end
    end)
end)
0
It works most of the time... I made a script that prints out idle time but it seems to be counting by 2. Sometimes it takes 6 seconds to kill the player. I also just want it to affect one person. But I guess I could figure out how to do it to each person. EzraNehemiah_TF2 3552 — 9y
0
I just noticed, thanks for this answer. This gave me an idea for another script! EzraNehemiah_TF2 3552 — 9y

Answer this question