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

Could someone please explain the problems with my kill brick?

I've been trying to learn more scripting and practice more often. My friend created a game and asked me to help, and he let me script (Its an obby game btw) I have been trying to make a brick that will at a random moment turn Neon and kill the player. This is the script I created and am confused why it isn't working, I really only need a little guidance on what I should fix and how to fix it.

Also, if I would need separate scripts for it tell me, but it would be nice if there were a way to avoid that

The script:

local noTrust = script.Parent
while true do
	wait(math.random(3,5))
	noTrust.Material = "Neon"
	noTrust.Touched:Connect(function(hit)
		local human = hit.Parent:FindFirstChild("Huamnoid")
		if human ~= nil then
			human.Health = human.Health - 100
		else
			wait(math.random(3,5))
			noTrust.Material = "Plastic"
		end
	end)
end


2 answers

✓ Accepted answer

You've got somewhat the right mentality, you just need a different approach to it. I would personally move the touched event out of the while loop, and just set a variable for when it's meant to kill. Here's how to properly do so, and feel free to ask questions:

local noTrust = script.Parent
local kill = false

noTrust.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) and kill then -- easy way to check if it's a player, and if kill == true
		hit.Parent:BreakJoints() -- equivalent to setting health to 0
	end
end)

while true do
	wait(math.random(3,5))
	noTrust.Material = Enum.Material.Neon -- My preferred method, though you can technically still just put "Neon"
	kill = true
	wait(math.random(3,5))
	kill = false
	noTrust.Material = Enum.Material.Plastic
end

*Note: I did not test this, but it's simple enough where I doubt there will be a bug.

Material is an Enum. You also spelt Humanoid wrong. Here is the fixed script:

local noTrust = script.Parent
while true do
	wait(math.random(3,5))
	noTrust.Material = Enum.Material.Neon
	noTrust.Touched:Connect(function(hit)
		local human = hit.Parent:FindFirstChild("Humanoid")
		if human then
			human.Health = 0
		else
			wait(math.random(3,5))
			noTrust.Material = Enum.Material.Plastic
		end
	end)
end

Don't forget to accept the answer if I helped!

2 comments

0

I am sorry DesertusX that I didn't mark your answer as the solution. It was my fault for not making it clear enough for you to see what I had in mind. You helped me understand my problems very well

0

No problem!

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