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

How to reference a model that has been passed into a function?

Asked by 4 years ago

I created a function as seen below that will destroy a model and then replace it with a model. It was my way of restoring a model back to its original colors, as I could not figure out another way to do so.

ChristmasTreeID = 4427277288

--Destroy one model and add in another one
local function replace(ID, model) 
    print(model)
    game.workspace.model:Destroy()            --Error occurs at this line
    wait(.1)
    local InsertService = game:GetService("InsertService")
    local modelContainer = InsertService:LoadAsset(ID)
    local newModel = modelContainer:GetChildren()[1]
    newModel.Parent = workspace
    modelContainer:Destroy()
end

replace(ChristmasTreeID, game.Workspace.ChristmasTree)

I get the following error at the indicated line: model is not a valid member of Workspace

The print statement prints out ChristmasTree, but then referencing it in the next statement does not work. I am new to Lua and Roblox, and am creating this game to teach some coding concepts to kids. I assume the error has something to do with how the parameter 'model' is passed in, but I am not sure. Any help would be appreciated!

1 answer

Log in to vote
1
Answered by 4 years ago

Your line:

game.workspace.model:Destroy()

Is supposed to be:

model:Destroy()

Right? I mean, otherwise you're trying to delete something named "model" that's in the workspace, rather than the model reference you passed to the function.

0
That makes perfect sense, and it worked. Thank you so much! -Best, Mason SenseiMason 2 — 4y
Ad

Answer this question