In my game, I have a part that represents a spawning zone for food, in that part, there is a normal server script that fires a function within a module script to spawn food, once that food is eaten, the food then disappears, but how do I tell the server script that initially fired the spawning function within the module script that the food has been eaten so the server script can spawn another food.
So far, I am currently using a ChildRemoved event but I have a feeling this method is poorly optimised, how would I go around this?
Server Script
spawnPart = script.Parent Food = game.ReplicatedStorage.Assets.Nut waitIntervals = 50 for i = 1, maxFood do petModule.GiveFood(Food, spawnPart, waitIntervals) -- petModule is the module that spawns the food using Clone() end
Module Script
module.GiveFood = function(FoodModel, spawnPart, waitIntervals) coroutine.resume(coroutine.create(function() local Food = FoodModel:Clone() foodTable[Food] = {} foodTable[Food]["Food"] = Food foodTable[Food]["currentPlr"] = nil foodTable[Food]["deSpawn"] = false foodTable[Food]["Food"].Parent = spawnPart local baseHealth = foodData[foodTable[Food]["Food"].Name]["Health"] local xPos = math.random(-spawnPart.Size.X/2, spawnPart.Size.X/2) local zPos = math.random(-spawnPart.Size.Z/2, spawnPart.Size.Z/2) local yPos = Food.Size.Y/2 - 0.1 foodTable[Food]["Food"].CFrame = spawnPart.CFrame * CFrame.new(xPos,yPos, zPos) * CFrame.Angles(0,math.random(1,36)*10,0) foodTable[Food]["Health"] = baseHealth foodTable[Food]["Food"].ClickDetector.MouseClick:Connect(function(Plr) if foodTable[Food]["currentPlr"] == nil then if Pets[Plr]["Robot"] then if Pets[Plr]["Feeding"] == nil then gatheringFood(Plr, foodTable[Food]) elseif Pets[Plr]["Feeding"] ~= nil then local preFood = Pets[Plr]["Feeding"] foodTable[preFood]["currentPlr"] = nil foodTable[preFood]["Food"].FoodHealth.Enabled = false gatheringFood(Plr, foodTable[Food]) end end -------------------------------- else if Plr == foodTable[Food]["currentPlr"] then foodTable[Food]["Health"] = baseHealth foodTable[Food]["Food"].FoodHealth.Enabled = false foodTable[Food]["currentPlr"] = nil PetGatherStop(Plr) end end end) wait(math.random(waitIntervals,waitIntervals + waitIntervals/2)) if foodTable[Food] then foodTable[Food]["deSpawn"] = true if foodTable[Food]["currentPlr"] == nil then foodTable[Food]["Food"]:Destroy() foodTable[Food] = nil end end end)) end