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

Touched script for several parts?

This question will sound very stupid, but ... how can I make my script detect several parts?

local deb = false

script.Parent.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h and not deb then
		local plr = hit.Parent.Name
		deb = true
		diamante.Transparency = 1
		game.Players[plr].leaderstats.Diamonds.Value = game.Players[plr].leaderstats.Diamonds.Value + 10
		game.Players[plr].XP.EXP.Value = game.Players[plr].XP.EXP.Value + 1
		wait(4)
		deb = false
		diamante.Transparency = 0
	end
end)

This script is inside a diamond and is executed by touching it, but I have a total of 20-30 diamonds with the same script in each one ..Any idea on how to use a single script for all diamonds?

Thanks!

1 answer

✓ Accepted answer

Say I have some parts grouped together in a Model. I can run a loop to connect each part's Touched event to the same function.

Model = game.Workspace.Model

func = function(part)
print(part.Name .. " was touched!")
end

for key, value in pairs(Model:GetChildren()) do
value.Touched:Connect(func(value))
end

2 comments

0

made a few mistakes in the first post, check for edits

0

on line 8, the parameter of function "func" isn't necessary, it is more commonly done as value.Touched:Connect(func)

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