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
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
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)