Hiya, I want to add floating and rotation to multiple food items, these models spawn randomly and can be duplicated.
I’m trying to make a script that waits for new items to be added to the Workspace and checks if they match the specified food names. If a match is found, it applies the desired behaviour
Can’t seem to get this to work
local foodNames = {"apple", "banana", "pizza"}
local function applyBehaviorToFood(part)
local partName = part.Name:lower()
for _, foodName in ipairs(foodNames) do
if partName == foodName then
-- Floating and rotating
local floatHeight = -1
local initialPosition = part.Position
local targetPosition = initialPosition + Vector3.new(0, floatHeight, 0)
local rotateSpeed = 45 -- degrees per second
while true do
part.CFrame = part.CFrame * CFrame.Angles(0, math.rad(rotateSpeed * 0.1), 0)
wait()
end
end
end
end
-- Connect the function to new food items being added to the Workspace
workspace.ChildAdded:Connect(function(newChild)
if newChild:IsA("Model") then
applyBehaviorToFood(newChild)
end
end)
local foodNames = {"apple", "banana", "pizza"}
local function applyBehaviorToFood(part)
local partName = part.Name:lower()
if foodNames[partName]
if partName == foodName then
-- Floating and rotating
local floatHeight = -1
local initialPosition = part.Position
local targetPosition = initialPosition + Vector3.new(0, floatHeight, 0)
local rotateSpeed = 45 -- degrees per second
while true do
part.CFrame = part.CFrame * CFrame.Angles(0, math.rad(rotateSpeed * 0.1), 0)
wait()
end
end
end
end
-- Connect the function to new food items being added to the Workspace
workspace.ChildAdded:Connect(function(newChild)
if newChild:IsA("Model") then
applyBehaviorToFood(newChild)
end
end)
local foodNames = {"apple", "banana", "pizza"}
local function applyBehaviorToFood(part)
local partName = part.Name:lower()
if table.find(foodNames,partName) then
local primary = part.PrimaryPart or part:FindFirstChildWhichIsA("BasePart")
if not primary then return end
local floatHeight = -1
local initialPosition = primary.Position
local targetPosition = initialPosition + Vector3.new(0, floatHeight, 0)
local rotateSpeed = 45 -- degrees per second
while true do
primary.CFrame = primary.CFrame * CFrame.Angles(0, math.rad(rotateSpeed * 0.1), 0)
task.wait()
end
end
end
-- Connect the function to new food items being added to the Workspace
workspace.DescendantAdded:Connect(function(newChild)
if newChild:IsA("Model") then
applyBehaviorToFood(newChild)
end
end)
I’ve added checks for if there already are some models in workspace.
Code:
local foodNames = {"apple", "banana", "pizza"}
local function applyBehaviorToFood(part)
local partName = part.Name:lower()
if table.find(foodNames, partName) then
-- Floating and rotating
local floatHeight = -1
local initialPosition = part.Position
local targetPosition = initialPosition + Vector3.new(0, floatHeight, 0)
local rotateSpeed = 45 -- degrees per second
while true do
part.CFrame *= CFrame.Angles(0, math.rad(rotateSpeed * 0.1), 0)
task.wait()
end
end
end
-- Connect the function to new food items being added to the Workspace
workspace.ChildAdded:Connect(function(newChild)
if newChild:IsA("Model") then
applyBehaviorToFood(newChild)
end
end)
for i, child in workspace:GetChildren() do
if child:IsA("Model") then
task.spawn(applyBehaviorToFood, child)
end
end
local foodNames = {"apple", "banana", "pizza"}
local function applyBehaviorToFood(part)
local partName = part.Name:lower()
if table.find(foodNames,partName) then
local primary = part.PrimaryPart or part:FindFirstChildWhichIsA("BasePart")
if not primary then return end
local floatHeight = -1
local initialPosition = primary.Position
local targetPosition = initialPosition + Vector3.new(0, floatHeight, 0)
local rotateSpeed = 45 -- degrees per second
while true do
primary.CFrame = primary.CFrame * CFrame.Angles(0, math.rad(rotateSpeed * 0.1), 0)
task.wait()
end
end
end
-- Connect the function to new food items being added to the Workspace
workspace.DescendantAdded:Connect(function(newChild)
if newChild:IsA("Model") then
applyBehaviorToFood(newChild)
end
end)
repeat task.wait(2.5) until game:IsLoaded()
for i, child in workspace:GetChildren() do
if child:IsA("Model") then
task.spawn(applyBehaviorToFood, child)
end
end
waits around 2-5 second until everything is loaded for all clients ig