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

Why is my script editing the same part instead of creating a new one?

So, right now my goal is to create a tool that makes bricks form a circle around my character when I click and got this far

local sp = script.Parent
local PartsToGenerate = 10

function BlastAttack()
	
	local Part = Instance.new("Part")
	Part.Anchored = true
	
	for Gen = 1, PartsToGenerate do
		Part.Parent = workspace
		Part.CFrame = CFrame.Angles (0, math.rad(360/PartsToGenerate*Gen), 0)
		wait()
	end
	
end

while wait(1)do
	BlastAttack()
end

My issue is the script rotating the generated parts instead of creating a new part for each 'Rotation Frame' I guess. If that makes sense.

I'm sorry if it's an easy fix Dx I'm just stupid.

1 answer

✓ Accepted answer

The problem is that the part is created outside the loop so you are just editing its properties instead of creating a new part which is what you need.

This Problem

local sp = script.Parent
local PartsToGenerate = 10

function BlastAttack()

    local Part = Instance.new("Part") -- part is created here
    Part.Anchored = true

    for Gen = 1, PartsToGenerate do -- you loop to make a circle but you do no create a new part
        Part.Parent = workspace
        Part.CFrame = CFrame.Angles (0, math.rad(360/PartsToGenerate*Gen), 0) -- why do 360/10? or why do this each time its the same math porblem
        wait()
    end

end

while wait(1)do
    BlastAttack()
end

The fix with some additions

local sp = script.Parent
local PartsToGenerate = 10

local num = 360/PartsToGenerate -- do some math here to save time in the loop

function BlastAttack()

    for Gen = 1, PartsToGenerate do 
	-- this now create a part each time the loop is ran 
	 local Part = Instance.new("Part",  game.Workspace)  -- second arg is optional, which is the pars parent
  	Part.Anchored = true
        Part.CFrame = CFrame.Angles (0, math.rad(num *Gen), 0) -- simpler math =  more efficient 
        wait()
    end

end

while wait(1)do
    BlastAttack()
end

Hope this helps.

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