Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 1 year ago

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.

0
Models do not have CFrames, you will need to use something like PivotTo or PrimaryPart. SamZeKat 28 — 1y

2 answers

Log in to vote
0
Answered by 1 year ago

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
0
Awesome! Thank you so much, It worked perfectly. Glad to learn something new! LionisTheKid 19 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

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)

Answer this question