Issues with applying local script to multiple parts

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)

I think you can just go:

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)

But by what’s wrong what does that mean?

try this

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)

video:

Needs to be local script. So I need it working inside the StarterPlayerScript

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

just put that script in a local script under starter player and add this at the end (from @Katrist )

for i, child in workspace:GetChildren() do
	if child:IsA("Model") then
		task.spawn(applyBehaviorToFood, child)
	end
end

Doesn’t seem to work - made sure the part is called ‘apple’ and placed the local script in starter player.

@Uniquedummie same goes for your script.

Maybe I misplaced something not too sure…

The code u listed there isn’t even mine, are you sure you copied it right??

Yep, Added the extra lines you mention above to your original comment.

under player Scripts

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