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 efficient damage bricks using only one script?

I've made a damage script for the kill parts in my obby,

function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
                human.Health = 0
        end
end
script.Parent.Touched:connect(onTouched)

but I want to eliminate most of these scripts to make sure that my obby runs as fast as possible.

Is there a way to make one script that detects when any of the kill parts (they are all grouped) are touched and then deal 100 damage to the humanoid that touched it?

I've tried putting all of the parts as children under the script and changed the script to say:

function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
                human.Health = 0
        end
end
script.Child.Touched:connect(onTouched)

but that didn't seem to work.

2 comments

0

ik this is probably a noob question, but i kinda new to programming

0

I don't think you are able to do that. An idea is to copy and paste the script on each brick.

1 answer

✓ Accepted answer
local DamageParts = workspace.Parts:GetChildren()

local function DealDamage(Touched)
	local Humanoid = Touched.Parent:FindFirstChildOfClass("Humanoid")
	if (Humanoid) then
		Humanoid:TakeDamage(Humanoid.Health*Humanoid.MaxHealth)
	end
end

for _,Part in pairs(DamageParts) do
	if (Part:IsA("BasePart")) then
		Part.Touched:Connect(DealDamage)
	end
end

3 comments

0

how would i make it so that its only the kill parts that are dealing damage, not every single part in the workspace with this script?

0

It wont. Simply just name the Grouped Model "Parts" and the code will do everything from there.

0

thx i modded the script now and it works perfectly :D

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