How to make a script that handles multiple Parts?

Hey Developers,
I made this Script in a Part which works fine! Here is the Script:

script.Parent.Touched:Connect(function(hit)
	local Player = game:GetService("Players"):FindFirstChild(hit.Parent.Name)
	local PlayerGui = Player.PlayerGui
	local RebirthButton = PlayerGui:WaitForChild("RebirthUi").TextButton
	local TextButton = PlayerGui:WaitForChild("RebirthUi").TextLabel
	if RebirthButton.Visible == false then
		TextButton.Visible = true
		RebirthButton.Visible = true
		PlayerGui:WaitForChild("RebirthUi").RebirthAmount.Value = script.Parent.Amount.Value
		Player:WaitForChild("leaderstats").RebirthAmount.Value = script.Parent.Amount.Value
		wait(5)
	else
		TextButton.Visible = false
		RebirthButton.Visible = false
		wait(5)
	end
end)

The Script is in a Part in Workspace. MY FIRST PROBLEM is that when the Gui closes this error in output appears but it still works… Workspace.Rebirth.Script:3: attempt to index nil with ‘WaitForChild’
THE SECOND THING is that I was wondering if I could make a local script to handle the parts (I have exact the same parts in Workspace but they all have different Values… the first problem is that I don’t know how to handle multiple parts with one script and the second problem is that the Values would get changed by the client… Any help would be appreciated!!!

Sever scripts (opposed to LocalScripts) cannot access the player’s gui like that, you have to change this script to a LocalScript.

As for handling multiple parts I would recommend using the Roblox Collection Service and the Tag Editor Plugin to tag objects inside the workspace.

The simplest example of the collection service looks like this, in a single script which on start up gets every object with the tag “RebirthTag” and applies an on touch function named onhitfunction.

for _, taggedPart in ipairs(collectionService:GetTagged("RebirthTag")) do
    taggedPart.Touched:Connect(onhitfunction)
end

More on the tag editor and why to use collection service

1 Like

like this?

local RebirthButton = script.Parent.Rebirth
local Information = script.Parent.Information
local debounce = true

for i,v in pairs(game.Workspace:GetChildren()) do
	if v.Name == "Rebirth" and v:IsA("Part") then
		v.Touched:Connect(function(hit)
			if hit ~= nil then
				local char = hit.Parent
				if char ~= nil then
					if debounce == true then
						if RebirthButton.Visible == false then
							debounce = false
							script.Parent.RebirthAmount.Value = game.Players.LocalPlayer:WaitForChild("leaderstats").RebirthAmount.Value
							game.ReplicatedStorage.ChangeRebirthAmount:FireServer()
							RebirthButton.Visible = true
							Information.Visible = true
							wait(5)
							debounce = true
						else
							debounce = false
							RebirthButton.Visible = false
							Information.Visible = false
							wait(5)
							debounce = true
						end
					end
				end
			end
		end)
	end
end

in the remote event that is fired should give the player the value of the touched part… How do I give the Remote Event that is fired the information tho? Thanks for your fast Reply!!!

Idea: I will add a function that changes the value of the rebirth amount in the leaderstats…

script.Parent.Touched:Connect(function(hit)
	local Player = game:GetService("Players"):FindFirstChild(hit.Parent.Name)
	Player:WaitForChild("leaderstats").RebirthAmount.Value = script.Parent.Amount.Value
	Player.PlayerGui.RebirthUi.RebirthAmount.Value = game.Players.LocalPlayer:WaitForChild("leaderstats").RebirthAmount.Value
end)

i will add this function in every rebirth part! Would this work?

I would recommend using the collection service instead of going through every part in the workspace, but if you are going to do that use workspace:GetDescendants() as this will get children of children.

1 Like

I tested everything and it worked! Thank you so much! the only thing is that the Output spams this error…
Workspace.Rebirth.Script:4: attempt to index nil with ‘WaitForChild’
it comes from the script in the parts…

do you know how to fix that error?

Sever scripts (opposed to LocalScripts) cannot access the player’s gui like that, you have to change this script to a LocalScript.

I need more information if this quote isn’t correct.

Ok so im using this line for a text:

while true do
	wait()
	script.Parent.Text = "Are you sure you want to Rebirth for "..script.Parent.Parent.RebirthAmount.Value.." Points?"
end

but if a local script changes the value it would be 0 :confused:

OH wait I can just use the value in the player called rebirth amount im so stupid. I will inform if it works!

Yep it works fine now! Only if I touched the Part it spams this error in output:
Workspace.Rebirth.Script:3: attempt to index nil with ‘WaitForChild’
it comes from the line 3 in this script:

script.Parent.Touched:Connect(function(hit)
	local Player = game:GetService("Players"):FindFirstChild(hit.Parent.Name)
	Player:WaitForChild("leaderstats").RebirthAmount.Value = script.Parent.Amount.Value
end)

fixed :smiley:

script.Parent.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid") 
	if Humanoid then
		Player:FindFirstChild("leaderstats").RebirthAmount.Value = script.Parent.Amount.Value
	end	
end)

I gave you the solution for your first reply btw!

1 Like