Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 9 years ago

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
0
You shouldn't have spaces for methods. M39a9am3R 3210 — 9y
0
It's not good style, but there's no real problem with putting spaces there (other than not doing it consistently) BlueTaslem 18071 — 9y
0
Line 3 should be v.ClickDetector.MouseClick assuming all the children have click detectors. NotsoPenguin 705 — 9y
0
ok, i made those changes but still dont work snipers0076 5 — 9y
View all comments (2 more)
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 snipers0076 5 — 9y
0
10:59:06.541 - Script 'Workspace.Tycoons.Brightblue.PurchasedObjects.VendingMachin', Line 5 snipers0076 5 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago
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

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. snipers0076 5 — 9y
0
Try local function onClicked() KilowattLaser 5 — 9y
Ad

Answer this question