-
What do you want to achieve?
I’d like to have a random model from the Shop folder in replicated storage, parent itself to the ObjectDisplay part of each SpecialDisplay object in the workspace. -
What is the issue?
The model only parented to the ObjectDisplay part for 1 SpecialDisplay object in the workspace, not all of the SpecialDisplay objects in the workspace.
-
What solutions have you tried so far?
Tried rewriting the code multiple times for a few hours… no success ._.
Additional Info + ServerScript
local replicatedStorage = game:GetService("ReplicatedStorage")
local shopFolder = replicatedStorage:WaitForChild("Shop")
local workspace = game:GetService("Workspace")
-- Find all the SpecialDisplay objects in the workspace
local specialDisplays = workspace:GetDescendants()
for _, obj in ipairs(specialDisplays) do
if obj.Name == "SpecialDisplay" then
-- Find all the ObjectDisplay children of each SpecialDisplay1 object
local objectDisplays = obj:GetDescendants()
for _, child in ipairs(objectDisplays) do
if child.Name == "ObjectDisplay" then
-- Spawn a random model for each ObjectDisplay child
local function spawnRandomModel()
local models = shopFolder:GetChildren()
if #models == 0 then
print("There are no models in the shop folder.")
return
end
local randomModelIndex = math.random(1, #models)
local randomModel = models[randomModelIndex]
local clonedModel = randomModel:Clone()
-- Find the root part of the cloned model
local rootPart = clonedModel.PrimaryPart.CFrame.Position
-- Calculate the offset between the root part and the ObjectDisplay child
local offset = child.Position - rootPart
-- Parent the cloned model to the ObjectDisplay child and move it to the correct position
clonedModel.Parent = child
clonedModel:SetPrimaryPartCFrame(CFrame.new(child.Position)) --+ offset))
end
-- Spawn a random model immediately for each ObjectDisplay child
spawnRandomModel()
-- Spawn a new random model for each ObjectDisplay child every 30 minutes
while true do
wait(30 * 60)
spawnRandomModel()
end
end
end
end
end
Let me know if you’d like me to provide additional info! Thank you for your help <3