ok, I have my script to work on my vending machines just fine.
I am now trying to add more script to it so when you click the button it will charge you a set amount (from leaderstats)(cash-storage)
I thought this was set up right but I guess I was wrong. ( I think I know were the problem is but I don't know what to put In it)
I think the problem is on line 3 (Children) but all the files in the parent of script have different names.
any help would be nice, thanks!
script.Parent:WaitForChildren() for i,v in pairs (script.Parent:GetChildren()) do v.ClickDetector.MouseClick:connect(function(Click) local player = game.Players:GetPlayerFromCharacter(Click.Parent) local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name) if cashmoney ~= nil then if cashmoney.Value >= v.Price.Value then cashmoney.Value = cashmoney.Value - v.Price.Value function onClicked(Name, Holder) local DOF = game.Lighting.DrinkandFood:findFirstChild(Name):clone() DOF.Parent = game.Workspace DOF.Handle.CFrame = Holder.CFrame DOF.Handle.Anchored = false end local c = script.Parent:GetChildren() for i = 1, #c do if c[i].className == "Part" then c[i].ClickDetector.MouseClick:connect(function() onClicked(c[i].Name, c[i].Holder)end) end wait(0.5) -- Time to wait in between clicks end end end end) end
script.Parent:WaitForChildren() //Connect all button functions for _, v in ipairs (script.Parent:GetChildren()) do //<- I'd use ipairs and you do not use the i variable v.ClickDetector.MouseClick:connect(function(Click) //Find player's wallet local player = game.Players:GetPlayerFromCharacter(Click.Parent) local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name) //Check for funds, and deduct price if cashmoney ~= nil then if cashmoney.Value >= v.Price.Value then cashmoney.Value = cashmoney.Value - v.Price.Value //When everything is verified function onClicked(Name, Holder) local DOF = game.Lighting.DrinkandFood:findFirstChild(Name):clone() DOF.Parent = game.Workspace DOF.Handle.CFrame = Holder.CFrame DOF.Handle.Anchored = false end //Find all children local c = script.Parent:GetChildren() for _, Test in ipairs(c) do //<- Partly preference, but ipairs is very sweet synactic sugar if Test:IsA("Part") then //<- So is IsA() onClicked(Test.Name, Test.Holder) //<- Don't connect with another click end wait(0.5) -- Time to wait in between clicks end end) end
Sorry for editing your formatting, but I was having a hard time understanding it. I also took the liberty to lightly comment the code for ease of understanding.
I did my best to repair the code without an error description or exactly what the application is, so I hope this helped.
--Cantos