In my upcoming game Make a Cake Remastered, I'm working on a mechanic that keeps track of the # of cakes made by a player. It works by inserting a IntValue named the playername of that player (ex: An IntValue named "thebenster" inside the cake block), and then passes an invisible brick at the end of the factory, and awards the player in the "Cakes Made" leaderstat, and deletes the Value. The real kicker is if there are multiple IntValues in the cake from multiple players, I want it to award all of them in the "Cakes Made" leaderstat. As of now, this script is unstable, and if there are multiple values from multiple players, it may only award one player, or no one at all. Is there a better way to write this script?
function onTouched(part)
if part.Name == "FinishedCake" then
local a = part:GetChildren()
for i = 1, #a do
if (a[i].ClassName == "IntValue") then
if game.Players:FindFirstChild(a[i].Name) == nil then return end
game.Players:FindFirstChild(a[i].Name).leaderstats:FindFirstChild("Cakes Made").Value = game.Players:FindFirstChild(a[i].Name).leaderstats:FindFirstChild("Cakes Made").Value +1
a[i]:remove()
else return end
end
end
end
script.Parent.Touched:connect(onTouched)
