I'm trying to clone a part, take away 3 from a value, and stop cloning the part and taking away the value when the value reaches 0

I want to make it so when the player touches a hitbox for a vending machine I made it spawns a part and takes away 3 from the coin value.
I’m not the best at making scripts so I did my best and eventually got this far.
I wasn’t able to figure out a solution for this. Usually a friend of mine would do most of these scripts while I would build things but now I have to figure out how to do this.
This is the current script I have:

local Part = script.Parent
local BloxyCola = game.ServerStorage.BloxyCola
local WaitTime1 = false
local WaitTime2 = false

local function ClonePart()
	if WaitTime1 == false then
		WaitTime1 = true
		local Clone = BloxyCola:Clone()
		Clone.Parent = game.Workspace
		Clone:MoveTo(script.Parent.Parent.Glass.Position + Vector3.new(-3, 0, 0))
		task.wait(1)
		WaitTime1 = false
	end
end

Part.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	Player.coins.Value = Player.coins.Value -3
end)

Part.Touched:Connect(ClonePart)

If I add a debouce to the part where the player loses coins then the player doesn’t lose coins at all.

Part.Touched:Connect(function(hit)
	if WaitTime2 == false then
		WaitTime2 = true
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		Player.coins.Value = Player.coins.Value -3
		task.wait(1)
		WaitTime2 = false
	end
end)

I don’t know how to add a debouce and still have the script take away 3 coins every time a part spawns from the vending machine. I will add a script a bit later that disables the other script when the coin value reaches 0 if I can figure out how to do it.

Here’s your code with the correct debounce:

local Part = script.Parent
local BloxyCola = game.ServerStorage.BloxyCola
local debounce = false

function ClonePart()
    local Player = game.Players:GetPlayerFromCharacter(Part.Parent)
    if Player.coins.Value >= 3 then
        local Clone = BloxyCola:Clone()
        Clone.Parent = game.Workspace
        Clone:MoveTo(Part.Parent.Glass.Position + Vector3.new(-3, 0, 0))
        Player.coins.Value = Player.coins.Value - 3
    end
end

Part.Touched:Connect(function(hit)
    local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if Player and Player.coins.Value >= 3 and not debounce then
        debounce = true
        Player.coins.Value = Player.coins.Value - 3
        wait(1)
        debounce = false
    end
end)

Part.Touched:Connect(ClonePart)
1 Like

the script you have written is not the best approach for this. You can use this instead

local Part = script.Parent
local BloxyCola = game.ServerStorage.BloxyCola
local WaitTime1 = false

local function ClonePart()
	local Clone = BloxyCola:Clone()
	Clone.Parent = game.Workspace
	Clone:MoveTo(script.Parent.Parent.Glass.Position + Vector3.new(-3, 0, 0))
end

Part.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player and not WaitTime1 then
		WaitTime1 = true
        Player.coins.Value -= 3
        ClonePart()
        Part.TouchEnded:Connect(function(hit2)
           if game.Players:GetPlayerFromCharacter(hit2.Parent) == Player then
               WaitTime1 = false
                return
           end
        end)
    end
end)
1 Like

I ended up removing WaitTime1 = false and created 2 vending machines because the player really only gets 6 coins total.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.