Script doesnt work, but theres no error

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    A “Builder” NPC that walks around randomly and places randomly-generated parts.
  2. What is the issue? Include screenshots / videos if possible!
    It doesnt place parts, but theres no error.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    None, yes, found nothing.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local placeNow=script.Parent.placeNow
function place()
	local b1=Instance.new("Part")
	Instance.new("Part")
	b1.Color=BrickColor.random()
	b1.Parent=workspace
	b1.Name="Brick"
	script.Shape.Value=math.random(1,4)
	if script.Shape.Value==1 then
		b1.Shape="Block"
	end
	if script.Shape.Value==2 then
		b1.Shape="Ball"
	end
	if script.Shape.Value==3 then
		b1.Shape="Cylinder"
	end
	if script.Shape.Value==4 then
		b1.Shape="Truss"
	end
	b1.Position=script.Parent.Head.Place.Position
end
while true do
	placeNow.Event:Connect(place)
	wait(math.random(1,5))
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

remove the while true loop because if an event is fired then it will connect to a function once it should be called. and also do b1.Anchored = true

1 Like

You do not need that while loop since the .Event will run when place now is fired, so you do not need to constantly check for it. Remove it

PS

You also don’t need to have script.Shape intvalue. Just generate a random number and assign it to a variable. Check that variable instead

Okay, but first i gotta tell: it places them around forever. Like a trowel with autoclicker.

Okay, but first i gotta tell: it places them around forever. Like a trowel with autoclicker.

1 Like

Unfortunately there were many issues that I could spot with your script, so it was more convenient for me to rewrite it for you:

local partType = Enum.PartType:GetEnumItems() -- Gets an array of each different part type

local function placePart() -- Local functions are preferable over global functions, unless you're coding a module
	local part = Instance.new("Part") -- For some reason you were creating 2 parts in the original script
	part.BrickColor = BrickColor.random() -- part.Color needs a Color3 value, not a BrickColor, so use part.BrickColor...
	-- ...if you want to use BrickColor.random()
	part.Name = "Brick"
	part.Position = Vector3.new(0, 8, 0) -- This is for debugging, in-order to make sure the parts are being created
	part.Shape = partType[math.random(#partType)] -- Sets the part's shape to a random type
	part.Parent = workspace -- Make sure you set an Instance's parent after you're done setting its properties!
end

while true do
	placePart() -- Call the placePart function whenever you want to generate a new part
	task.wait(math.random(5)) -- Using math.random with only one value will automatically set 1 as the minimum
end -- Using task.wait is preferable over wait, since wait is limited to run at 30fps and isn't able to directly talk to the task sheduler

I wrote as many detailed comments as I could think of in-order to help explain some of the issues in the original script, and how the new script works :slight_smile::+1:

Could you remove the spaces in the equals signs and more? They honestly waste space for me…

I’d recommend keeping them since it makes code much easier to read and debug, but here’s a version without spaces:

local partType=Enum.PartType:GetEnumItems()--Gets an array of each different part type

local function placePart()--Local functions are preferable over global functions, unless you're coding a module
	local part=Instance.new("Part")--For some reason you were creating 2 parts in the original script
	part.BrickColor=BrickColor.random()--part.Color needs a Color3 value, not a BrickColor, so use part.BrickColor...
	--...if you want to use BrickColor.random()
	part.Name="Brick"
	part.Position=Vector3.new(0,8,0)--This is for debugging, in-order to make sure the parts are being created
	part.Shape=partType[math.random(#partType)]--Sets the part's shape to a random type
	part.Parent=workspace--Make sure you set an Instance's parent after you're done setting its properties!
end

while true do
	placePart()--Call the placePart function whenever you want to generate a new part
	task.wait(math.random(5))--Using math.random with only one value will automatically set 1 as the minimum
end--Using task.wait is preferable over wait, since wait is limited to run at 30fps and isn't able to directly talk to the task sheduler

Ill try the new code! I just dont like them because they waste data and space cuz it counts as a unicode..

Works! Thanks for the help!! :smile:

1 Like

IIRC there are plugins available that can handle removing spaces for you, although do be sure to check that they’re from a reputable source before installing them

K, also, you forgot the size property… But no problem-o, ill just add it back!

1 Like

New issue, i cant make the size random… I tried using :ScaleTo(), but it did not work.

	b:ScaleTo(math.random(5,12),math.random(3,10),math.random(2))

“b” is the variable for the part.

1 Like

Vector3 doesn’t have a built-in random function, so you’ll need to do this instead (line 9 is where the size is being set):

local partType=Enum.PartType:GetEnumItems()--Gets an array of each different part type

local function placePart()--Local functions are preferable over global functions, unless you're coding a module
	local part=Instance.new("Part")--For some reason you were creating 2 parts in the original script
	part.BrickColor=BrickColor.random()--part.Color needs a Color3 value, not a BrickColor, so use part.BrickColor...
	--...if you want to use BrickColor.random()
	part.Name="Brick"
	part.Position=Vector3.new(0,8,0)--This is for debugging, in-order to make sure the parts are being created
	part.Size=Vector3.new(math.random(5,12),math.random(3,10),math.random(2))
	part.Shape=partType[math.random(#partType)]--Sets the part's shape to a random type
	part.Parent=workspace--Make sure you set an Instance's parent after you're done setting its properties!
end

while true do
	placePart()--Call the placePart function whenever you want to generate a new part
	task.wait(math.random(5))--Using math.random with only one value will automatically set 1 as the minimum
end--Using task.wait is preferable over wait, since wait is limited to run at 30fps and isn't able to directly talk to the task sheduler
1 Like

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