Why won't my script work?

FOR CONTEXT: I am making an adventure game and I disabled all the ways to die (reset character, etc)… Your not supposed to be able to die in the game with the precautions I took. But for exploiters, however, I put a bunch of invisible kill-bricks in the outside of the map so if they wanna fly or noclip, this script will crash their game.

I chose crashing the game because I want the exploiters to have a hard time.

My Code:

local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

local function Ondeath()
	while true do
		print("Humanoid Died")
	end
end

if humanoid.Health <= 0 then
	Ondeath()
end

Please let me know how to fix it. Thank you.
2 Likes

i’m not sure if it’s a good idea to create an anticheat in a localscript, as the client can just easily see the code and (i’m pretty sure) delete it. but i’m pretty sure you’d need to be constantly checking the humanoids health via while loop to fix this problem.

2 Likes
local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

local function Ondeath()
	while true do
		print("Humanoid Died")
	end
end

while true do
         wait()
	if humanoid.Health <= 0 then
		Ondeath()
	end
end
local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

local function Ondeath()
	while task.wait() do -- While true will cause immense lag.
		print("Humanoid Died")
	end
end

game:GetService("RunService").Heartbeat:Connect(function()
   if humanoid.Health <= 0 then
   	   Ondeath()
   end
end)

I have something similar to this now, but when I respawn, the lag stops… How do I fix it when you respawn, the lag doesnt stops

local function Ondeath()
	while task.wait() do -- While true will cause immense lag.
		print("Humanoid Died")
	end
end

literally breaks the crashing…
Change it to this.

local function Ondeath()
	while true do
		print("Humanoid Died")
	end
end

The best way to find when the Humanoid died is to use the built in event Died in every Humanoid

local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

humanoid.Died:Connect(function()
	print("humanoidDied")
end)

Generally speaking, do not ever use a loop to detect when things change, as it is very inefficient. When you do find a time to use a loop, be sure to add a yield/wait in the loop to prevent game-breaking lag. Hope this helps :grin:

4 Likes

I do feel like noting that @PerceptualReality has a point that exploiters can just find and remove the script if it’s a local script. You should add a fallback that has the server kick them after 15-20 seconds anyways. And of course, do that check on the server and not just pass it through a remote event.

local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

local function Ondeath()
	while true do
		print("Humanoid Died")
        Instance.new("Part", game:GetService("Workspace")) -- Causes Roblox to crash
	end
end

game:GetService("RunService").Heartbeat:Connect(function()
   if humanoid.Health <= 0 then
   	   Ondeath()
   end
end)