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

How do I make this killbrick toggleable so it can be insta-kill or not?

How do I make is so that the killbrick can be either insta-kill or not via a bool value? I tried to do this several times but the killbrick does not kill the player or the player takes non-instant kill damage. Is there any way I can do something like this?

Note: non instant kill means that the player takes damage but the player isnt killed instantly.

local Debounce = false

for _,v in pairs(script.Parent:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit) 
			if Debounce == false then
			Debounce = true
			local Player = hit.Parent:FindFirstChild("Humanoid")
				if Player then
					Player.Health = Player.Health - 30
					wait(0.5)
					Debounce = false
				end
			end
		end)
        end
end

Thanks for reading!

2 answers

✓ Accepted answer

I just wanna add to BazementBoyz answer.

local Debounce = false
local Toggle = BoolValue.Value --If the value is true then it kills, if false then it hurts.

for _,v in pairs(script.Parent:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			if not Debounce then
				if Toggle then --If toggle is true, humanoid's health is set to 0
					Debounce = true
		                	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
		                	if Humanoid then
		                    	Humanoid.Health == 0
					wait(0.5)
					Debounce = false
				else --If toggle is not true, humanoid's health is reduced by 50
					Debounce = true
					local Humanoid = hit.Parent:FindFirstChild("Humanoid")
		                	if Humanoid then
		                    	Humanoid.Health = Humanoid.Health - 50
					wait(0.5)
					Debounce = false
		                end
			end
		end)
	end
end

1 comment

0

I don't know why it looks so dirty but it's not my fault ;(

I'd like to address a couple of things before fixing your script.

the player takes non-instant kill damage

Player.Health = Player.Health - 30

Your debounce and this part taking the player's health by 30 is the reason why it's not instant killing.

if Debounce == false then

You can also use if not Debounce then. Nothing lost or gained, just slightly more convinient but it's preference.

local Player = hit.Parent:FindFirstChild("Humanoid")
                if Player then

That isn't the Player but the Players Character's Humanoid object. For future reference don't get the triple mixed PLEASE.

Now you can use BoolValue totoggle it.

BoolValue has 2 possible values, true and false. You can check whether it's either of those and continue the code depending if the value meets your criteria.

I've fixed the things that I address above and added an example pseudo toggle here:

local Debounce = false
local Toggle = BoolValue.Value --lets say its true, meaning the bricks kills

for _,v in pairs(script.Parent:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit) 
			if Toggle and not Debounce then
				Debounce = true
				local Humanoid = hit.Parent:FindFirstChild("Humanoid")
				if Humanoid then
					Humanoid.Health == 0
					wait(0.5)
					Debounce = false
				end
			end
		end)
	end
end

1 comment

0

what i meant is to make it instant kill or non instant kill, non instant kill means the player takes damage but doesnt get killed instantly. nice explanations tho.

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