Vending machine not working

What this is supposed to do is simple: When you click on the vending machine, a dollar gets cloned to the machine which then automatically gets put inside.
This has a 5 second cooldown each time.
There is no currency leaderboard of any type

What I get instead is this robloxapp-20210523-1655365.wmv (3.7 MB)

If you don't trust downloads or don't understand what's going on

I walk up to the vending machine and click on the spot where you insert money. Even though it places, there is no cooldown in-between placement and it does not enable it’s scripts (See below)

  • The folder “MoneyBox” will contain the cloned cash that is put inside.

  • The vending machine model is a child of Workspace

  • Hitboxes are only there because the main vending machine is mostly a union.
    Capture

  • “Money” is a child of the vending machine.

  • It is the block that will be cloned to “MoneyBox”

  • It’s clone, “ClonedCash” will have these scripts enabled in-order to properly animate and destroy itself

Capture

The first script comes from the “InsertRobux” Forgive my multiple word usage such as money, cash, robux, etc. They all mean the same thing.

Explanation of what it's SUPPOSED to do (Stuff in bold means it is working as of now)
  • The Boolean tells you that if it is true, the code can run on click.
  • On click, that Boolean will automatically turn false meaning that any more clicks will do nothing.
  • Money’s clone will: Be named ClonedCash, Have a new position of right infront of the machine, have it’s parent as Moneybox, and have it’s move and destroy script enabled.
  • After 5 seconds, the Boolean will be true meaning the cooldown is over
local InsertBoolean = true

if InsertBoolean == true then
	local function onClick()
		print("Money has been inserted")
		local InsertBoolean = false
		local original = script.Parent.Parent.Parent.Money
		local copycash = original:Clone()
		copycash.Name = "ClonedCash"
		copycash.Position = Vector3.new(14.13, 4.314, 19.449)
		copycash.Parent = script.Parent.Parent.Parent.Moneybox
		if copycash:FindFirstChild("Movescript")then
			copycash.Movescript.Enabled = true
		end
		if copycash:FindFirstChild("DestroyScript")then
			copycash.Destroyscript.Enabled = true
		end
		wait(5)
		local InsertBoolean = true
		print("Money is ready to be reinserted again")
	end
	script.parent.ClickDetector.MouseClick:Connect(onClick)
end

The second script comes from “Movescript”, a child of Money/ClonedCash

while true do
	wait(1)
	for i= 1, 200 do
		script.Parent.CFrame = script.Parent.CFrame * CFrame.new(1, 0, 0) --this part I can fix myself as it is most likely incorrect as well
		wait()
	end 
	wait(1)
	for i= 1, 200 do
		script.Parent.CFrame = script.Parent.CFrame * CFrame.new(1, 0, 0)
		wait()
	end 
end

The final script comes from “Destroyscript”, a child of Money/ClonedCash

wait(5)
script.Parent:Destroy()

Also keep in mind that even though I somewhat made all of this, I am not a very good scripter.

Why did you make separate scripts for click move and destroy? you can just make them functions and combine them into one script

local function onClick()
    --clone cash
    --move cash
    --destroy cash
end
1 Like

how would i be able to fix the Boolean then

the boolean shouldn’t be affected since its not anywhere in move and destroy

This worked. It is now destroying and making the animation as normal. However

What I mean by that is how do I stop the spam as seen in the video

My new script (in the InsertRobux block. The other 2 scripts were deleted)
local InsertBoolean = true

if InsertBoolean == true then
	local function onClick()
		print("Money has been inserted")
		local InsertBoolean = false
		--Properties
		local original = script.Parent.Parent.Parent.Money
		local copycash = original:Clone()
		copycash.Name = "ClonedCash"
		copycash.Position = Vector3.new(14.13, 4.314, 19.449)
		copycash.Parent = script.Parent.Parent.Parent.Moneybox
		
		--Movable
		for i = 1, 10, 1 do
			copycash.CFrame = copycash.CFrame * CFrame.new(0, 0, 0.1)
			wait(0.01)
		end 
		
		--Ending
		local InsertBoolean = true
		print("Money is ready to be reinserted again")
	end
	script.parent.ClickDetector.MouseClick:Connect(onClick)
end

I think the problem is that on line 6 you are declaring another InsertBoolean, so when you set it it only affects the InsertBoolean within the function and not changing the real one, just remove the local and it should fix it
snip
this is pretty much what is happening right now

EDIT: theres another in the ending part as well

Yeah I had to go yesterday no big deal srry

The Boolean is working. But how would it be so that way it can actually affect the click? Without the rest of the script, I have it as of now:

local insertBoolean = true
local function change()
	local insertBoolean = false
	print(insertBoolean)
end

local function onClick()
	if insertBoolean == true then
		change()
		--[[full script here
		
		]]--
		change()
	end
end

script.Parent.ClickDetector.MouseClick:Connect(onClick)

The print says it is false. Which is expected because I have not placed in a true switch yet. However, it still is able to be used even while false as well as it is still spammable.

Sorry if my code confused you, it was to demonstrate what went wrong with your code, not the fix
You can just do something like this

local bool = false

local function onClick()
    if not bool then
        bool = true
        -- Clone cash and play animation
        wait(cooldown)
        bool = false
    end
end

ClickDetector.MouseClick:Connect(onClick)
1 Like