How to make the block change color and cause damage when touching

I wanted a script for a barbed wire that when touched, take 25 damage and the color changes to red as indicative.

You can simply use an onTouched event, and then take the Humanoid health, and minus 25 from the value.

Example:

part.Touched:Connect(function(touchedpart) --this will only work if it is a part, or another instance with the .Touched event.

    if touchedpart.Parent:FindFirstChild("Humanoid") then --a check to make sure the touched part is inside the character model 
    touchedpart.Parent:FindFirstChild("Humanoid"):TakeDamage(25) --damage the player
    end

end)

You can then also change the brick colour in the touched event to red. A similar thread can be found here:

1 Like

A simple way to do that is by using the touched event. You would need a debounce variable set up so it will damage once, wait a bit, and then damage again.

In your touched event, check if whoever touched is a player or has a humanoid if you want it to also work for npcs, and if they are and the debounce is not a certain boolean, invert the boolean, change the color, damage them, wait a bit and set the debounce again so that way it can be reused

An example would be this script placed inside of the barbed wire

local part = script.Parent
local color = part.BrickColor

local deb = false

part.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if deb or not hum then return end
	deb = true
	part.BrickColor = BrickColor.Red()
	hum:TakeDamage(25)
	wait(1)
	part.BrickColor = color
	deb = false
end)

Alternatively since the color changes, you can jsut check if the color is not red to continue instead of making a debounce variable, but this is to give an example of how debounces are typically done

1 Like
db = true
color = script.Parent.Color
script.Parent.Touched:Connect(function(hit)
   humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
 if humanoid and db == true then
db  = false
humanoid.Health = humanoid.Health - 25
script.Parent.BrickColor = BrickColor.new("Really red")
wait(1)
db = true
script.Parent.Color = color
end
end)

Iā€™m getting an error: colorbrick is not a valid member of model workspace.structure.house

The error is indicating that the script is unable to find something called ā€œcolorbrickā€ inside of ā€œworkspace.structure.houseā€.

Ensure that youā€™ve spelled and capitalized ā€œcolorbrickā€ the exact same way it appears in the explorer and that itā€™s placed inside of the correct place that youā€™re trying to reference in the script.

If thereā€™s the possibility that the ā€œcolorbrickā€ will not be in that place when it is referenced from the script, consider using FindFirstChild or WaitForChild inside of a conditional statement to ensure that it exists before continuing.

I tried to do what you said and the error stopped, but the script simply doesnā€™t work when touching the barbed wire

What code did you set up and where is the script located?

image

And what is the code in that script?

local part = game.Workspace:FindFirstChild("BarbedCoil", true)
local color = part.BrickColor

local deb = false

part.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if deb or not hum then return end
	deb = true
	part.BrickColor = BrickColor.Red()
	hum:TakeDamage(25)
	wait(1)
	part.BrickColor = color
	deb = false
end)

Iā€™m using your script, but I made a small modification

The issue is that you have multiple of those BarbedCoil things, so the code only works for one of them. are you getting any errors? Youā€™ll have to loop through the BarbedWire folder and put the touched event on all of them

Iā€™m not getting any errors, the script just doesnā€™t work.

Try this out so itā€™ll work for all the barbed coils

local parts = --Location of BarbedWire folder

for _,part in pairs(parts:GetChildren()) do
	local color = part.BrickColor
	part.Touched:Connect(function(hit)
		local hum = hit.Parent:FindFirstChild("Humanoid")
		if part.BrickColor == BrickColor.Red() or not hum then return end
		part.BrickColor = BrickColor.Red()
		hum:TakeDamage(25)
		wait(1)
		part.BrickColor = color
	end)
end

An alternative, if your barb wire parts dont contain anything inside them is union. So union the parts together and connect the single touched event to it.

Iā€™m getting another error: BrickColor is not a valid member of mesh

Did you reference the correct folder? What is your code right now?

local parts = game.Workspace:FindFirstChild("BarbedCoil")

	for _,part in pairs(parts:GetChildren()) do
	local color = part.BrickColor
	part.Touched:Connect(function(hit)
		local hum = hit.Parent:FindFirstChild("Humanoid")
		if part.BrickColor == BrickColor.Red() or not hum then return end
		part.BrickColor = BrickColor.Red()
		hum:TakeDamage(25)
		wait(1)
		part.BrickColor = color
	end)
end

image

Reference the BarbedWire Model, not the BarbedCoil

Other error: Attempt to index nil with ā€˜GetChildrenā€™