[SOLVED] Compromising Folder > Part to Apply to Multiple Parts

I am trying to make this Script in the placement of ServerScriptService reference the Battery which is associated in the Workspace > Map > KillArea > BatterySpawn > Battery x2. How will I factor into making this set work properly.

ServerScriptService Script:

-- BatteryLocation Script
local folder = workspace.Map.Zones.BatteryZone -- Compromises the "Battery" SpawnPoint.
local tool = game.Workspace.Map.KillArea.BatterySpawn.Battery -- Original "Battery" SpawnPoint.
local toolSpawns = folder:GetChildren() -- Calling the function for the "Battery"

function batteryRespawn() -- idk lol
    local clone = tool:Clone() -- This will be creating a Clone for "Battery" to Spawn.
    clone.CFrame = toolSpawns[math.random(1,#toolSpawns)].CFrame + 
        Vector3.new(2,1,0) -- Picks one random Spawns CFrame with Y offset of 1 stud.
    clone.Orientation = Vector3.new(0, 0, 90) -- Setting the "Battery" rotation to 90 degrees.
	-- task.wait(math.random(15,25)) -- Waiting a random Value till Spawn.
    clone.Parent = workspace -- Cloning the "Battery" into the Workspace (Game Client and Server).
end
task.wait(4) -- Wait 4 seconds then will Anchor "Battery".
clone.Anchored = true -- Anchors the "Battery".

game:GetService("ServerStorage").BatteryPickUp.BatteryRespawn.Event:Connect(batteryRespawn) -- Functioning the "Battery" when picked up by Player.

batteryRespawn() -- Respawning the Battery after the Player will equip the "Item".

This part here is where it is finding the “Battery” Original Storage.

local tool = game.Workspace.Map.KillArea.BatterySpawn.Battery
1 Like

Hi there,

I’m slightly struggling to understand what exactly you’re trying to achieve here.

As you mentioned you have 2 instances of Battery inside of your BatterySpawn, I’m assuming you’re trying to run the batteryRespawn() function on both of the batteries instead of just one that you’re referencing at the top of your script?

1 Like

Well, yes and no I am trying to reference both Batteries going into the BatterySpawn. but it is only picking up the one Battery.

1 Like

I see. Why not simply for loop through an array of all the children of your BatterySpawn and execute your code on each battery upon each iteration of the loop?

Give the below code a shot and see if that points you in the right direction:

-- BatteryLocation Script
local folder = workspace.Map.Zones.BatteryZone -- Compromises the "Battery" SpawnPoint.
local BatterySpawn = game.Workspace.Map.KillArea.BatterySpawn -- Original "Battery" SpawnPoint.
local toolSpawns = folder:GetChildren() -- Calling the function for the "Battery"

function batteryRespawn() -- idk lol
	for _,tool in pairs(BatterySpawn:GetChildren()) do
		if tool.Name == "Battery" then
			local clone = tool:Clone() -- This will be creating a Clone for "Battery" to Spawn.
			clone.CFrame = toolSpawns[math.random(1,#toolSpawns)].CFrame + 
				Vector3.new(2,1,0) -- Picks one random Spawns CFrame with Y offset of 1 stud.
			clone.Orientation = Vector3.new(0, 0, 90) -- Setting the "Battery" rotation to 90 degrees.
			-- task.wait(math.random(15,25)) -- Waiting a random Value till Spawn.
			clone.Parent = workspace -- Cloning the "Battery" into the Workspace (Game Client and Server).
			
			task.wait(4) -- Wait 4 seconds then will Anchor "Battery".
			clone.Anchored = true -- Anchors the "Battery".
		end
	end
end

game:GetService("ServerStorage").BatteryPickUp.BatteryRespawn.Event:Connect(batteryRespawn) -- Functioning the "Battery" when picked up by Player.

batteryRespawn() -- Respawning the Battery after the Player will equip the "Item".
1 Like

Well they do both Spawn it the correct spot however, I cannot pick them up.

1 Like

@ZEDDxR sorry for the inconvenience it does work, I just stupid and didn’t mention the Tool whereabouts.

Don’t worry, happens to the best of us. Is everything fine working fine now?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.