Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Help!(Still No Answer)Can someone help me with this vending machine button (Charge for drink)?

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




























6 comments

0

You shouldn't have spaces for methods.

0

It's not good style, but there's no real problem with putting spaces there (other than not doing it consistently)

0

Line 3 should be v.ClickDetector.MouseClick assuming all the children have click detectors.

0

ok, i made those changes but still dont work

0

ok it gave me an error about the wait for child so i removed it. now it is giving me an error on the clickeddetector, but all buttons have one...10:59:06.541 - ClickDetector is not a valid member of Script

0

10:59:06.541 - Script 'Workspace.Tycoons.Brightblue.PurchasedObjects.VendingMachin', Line 5

1 answer

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

2 comments

0

It still comes back with the same error, and it saysthat on line 16 should be local. hmm, every one of the buttons has a clickdetector so i wonder why its not working.

0

Try local function onClicked()

This discussion is preserved as it was. New answers, comments, and votes are closed.