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

How can I make my script work with multiple parts with the same name?

Hello! I'm making an obby and I have a script in ServiceScriptService that determines whether or not you will be killed by a kill part if you have a certain gamepass. However, the script only works with one kill brick, not all. So, I'm wondering how I can make this script work with every part with the same name.

Here's the script that I have.

local id = (id here)

game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(plr,ido,purchased)
	if purchased and id == ido then
		game.Workspace.LavaBrick.Script.Disabled = true
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId.id)then
		game.Workspace.LavaBrick.Script.Disabled = true
	end
end)

1 answer

✓ Accepted answer

You could do something like this:

local killPartFolder = game.Workspace.killParts --actually just the path to the folder with the killparts in it

for i, part in pairs (killPartFolder:GetChildren()) do
	--run code for everey killpart
end

i recommend to put all the killParts in one Model or Folder so you can acess all parts

so just put this code into your PromptGamePassPurchaseFinished Event and run this into the for loop

game.Workspace.LavaBrick.Script.Disabled = true

simply just fot the for loop i showed you into the Event and the if statement and run the code you want (disabled = true)

like this:

local id = (id here)

game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(plr,ido,purchased)
    if purchased and id == ido then
        for i, part in pairs (killPartFolder:GetChildren()) do
			part.Script.Disabled = true
		end
    end
end)

game.Players.PlayerAdded:Connect(function(plr)
    if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId.id)then
        for i, part in pairs (killPartFolder:GetChildren()) do
			part.Script.Disabled = true
		end
    end
end)

1 comment

0

Okay, thank you!

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