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

I'm developing a roblox game and I'm having an issue with an this Random Item Spawner code?

The specific problem I'm having is specifically with CFrame...

local Spawners = game.Workspace.Spawners:GetChildren()

local Arrow_ = game.ServerStorage:FindFirstChild('Items')

local Arrow = Arrow_:FindFirstChild('ArrowModel')

function SpawnArrow()
	local NewArrow = Arrow:Clone()
	local ArrowSpawn = Spawners[math.random(1, #Spawners)]
	
	 ArrowSpawn.CFrame = Arrow.CFrame
	
	NewArrow.Parent = workspace
end

while true do 
	SpawnArrow()
	
	wait(5)
end

On line 11 whenever I run the code I get a reoccurring issue that states, "CFrame is not a valid member of Model "ServerStorage.Items.ArrowModel"" and I don't know how to exactly change or fix this.

1 comment

0

Models do not have CFrames, you will need to use something like PivotTo or PrimaryPart.

2 answers

✓ Accepted answer

Just as @SamZeKat said, models don't have a CFrame. Thankfully, because of the new updates, Roblox added the function to set and get a Pivot of a Part and Model. This is very important for models so you don't need to set a PrimaryPart anymore. If a model doesn't have a PrimaryPart, its pivot is in the middle. Keep in mind you can edit it.

To get the CFrame of a Model, just use Model:GetPivot() and instead of ArrowSpawn.CFrame = Arrow.CFrame, you should use NewArrow:PivotTo(ArrowSpawn:GetPivot()) since they're both Models (i assume), you tried to position a random spawn in an instance inside ServerStorage, and you teleport the spawn to the arrow instead of the arrow teleporting to a spawn.

local Arrow_ = game:GetService("ServerStorage").Items
local Arrow = Arrow_:FindFirstChild("ArrowModel")

function SpawnArrow()
	local Spawners = workspace.Spawners:GetChildren()
	
	local NewArrow = Arrow:Clone()
	local ArrowSpawn = Spawners[math.random(1, #Spawners)]
	
	NewArrow:PivotTo(ArrowSpawn:GetPivot())

	NewArrow.Parent = workspace
end

while task.wait(5) do 
	SpawnArrow()
end

1 comment

0

Awesome! Thank you so much, It worked perfectly. Glad to learn something new!

ok so assuming that you have your primary part set in your model(primary part is in the properties of the model, just choose a random part thats gonna be the base of the item)

local arrowcframe = Arrow.CFrame
NewArrow.Parent = workspace
ArrowSpawn:PivotTo(arrowcframe)

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