Click here for an example of what I'm talking about.
I was attempting to create a model previewer within one of my games, so players could preview models. The system worked very well at first until I started adding more and more items inside of the shop. (The models constantly updating cause tons of lag on the client) The script works by cloning one of the models from the shop into the previewer located on the right and constantly changing the model in order for the rotation script inside of it to work. I want to know if there is any way to update the camera without having to delete the old model inside of the Viewport frame and replacing it with a new one.
This is a script that I have inside of one of the shop items:
while wait() do ModelPreview:ClearAllChildren() ModelToReplace:Clone().Parent = ModelPreview end
The model preview basically mentions the viewport then clears all of the models inside of it and then the ModelToReplace replaces the old model.
If there is any way I could get it to constantly update it without having to delete the previous model and replacing it please let me know!
Your code is almost right; the lag is because you don't need to perform that operation so frequently (you're currently cloning the model 30x/second, even if it doesn't change!)
I'm also guessing that because you're recloning it constantly, the rotation script is only getting to rotate it once before you replace it again.
Instead of repeating that code in a while
loop, you need to run it only when one of the shop item buttons is Activated:
local function update(ModelToReplace) ModelPreview:ClearAllChildren() ModelToReplace:Clone().Parent = ModelPreview end for _, obj in ipairs(somewhere:GetChildren()) do -- Modify 'somewhere'; the point of this for loop is to connect each shop button to the 'update' function if obj:IsA("ImageButton") then -- You will need to modify this line; are they ImageButtons, Frames, ViewportFrames? obj.Activated:Connect(function() -- Note: Activated works for buttons but not Frames/ViewportFrames update(obj.Model) -- or however you get the ModelToReplace from the button end) end end
(If you aren't comfortable with tables yet and have a separate script for each shop button, another tactic is to just copy/paste lines 2 and 3 of your original script into each shop button script, modifying them of course so that ModelToReplace
refers to what it should in each case.)