Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I make a convenient Idle player kill script?

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

✓ Accepted answer

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

4 comments

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.

0

It still glitches a little though.

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"?

0

Yep

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)

2 comments

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.

0

I just noticed, thanks for this answer. This gave me an idea for another script!

This discussion is preserved as it was. New answers, comments, and votes are closed.