Second clone not working

Hello,

So i’m working on something that when a player clicks a button that the first clone gets cloned.
But when the player clicks it for the second time the other model needs to be cloned but that doesnt works.

Here’s the script;

local hover = game.SoundService.HoverSound

script.Parent.MouseEnter:Connect(function()
	hover:Play()
end)

local player = game.Players.LocalPlayer
local Cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
local leaderstats = player.leaderstats

script.Parent.MouseButton1Click:Connect(function()
	local second = false
	script.Parent.PurchaseScript.Disabled = true
	print("Script Disabled!")
	
	Cash.Value = Cash.Value - 500
	
	script.Parent.Parent.Visible = false
	
	wait(2.5)
	
	if second == false then
		local tvmakerclone1 = game.ReplicatedStorage.TvMakers.TvMakerLVL1:Clone()
		tvmakerclone1.Parent = game.Workspace
		second = true
	else
                --here needs to be the second clone 
		print("LOL")
	end
	
	wait(30)
	script.Parent.PurchaseScript.Disabled = false
	print("Script has been enabled!")

end)

Please help me with this!

1 Like

The issue is that the variable, second, resets every time the event has been fired. You’d need to put local second = false above it.

local second = false
script.Parent.Activated:Connect(function()
if not game.Workspace:FindFirstChild("TvClone1") then
    local tvmakerclone = game.ReplicatedStorage.TvMakers.TvMakerLVL1:Clone()
    tvmakerclone.Name = "TvClone1"
	tvmakerclone.Parent = game.Workspace
elseif game.Workspace:FindFirstChild("TvClone1") and not game.Workspace:FindFirstChild("TvClone2") then
    local tvmakerclone = game.ReplicatedStorage.TvMakers.TvMakerLVL1:Clone()
    tvmakerclone.Name = "TvClone2"
	tvmakerclone.Parent = game.Workspace
end

If there is multiple clones for each players, you can change that:

tvmakerclone.Name = Player.Name… “TvClone1”
if not game.Workspace:FindFirstChild(Player.Name… “TvClone1”) then

Like this?

local hover = game.SoundService.HoverSound

script.Parent.MouseEnter:Connect(function()
	hover:Play()
end)

local player = game.Players.LocalPlayer
local Cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
local leaderstats = player.leaderstats
local second = false
script.Parent.MouseButton1Click:Connect(function()
	
	script.Parent.PurchaseScript.Disabled = true
	print("Script Disabled!")
	
	Cash.Value = Cash.Value - 500
	
	script.Parent.Parent.Visible = false
	
	wait(2.5)
	
	if second == false then
		local tvmakerclone1 = game.ReplicatedStorage.TvMakers.TvMakerLVL1:Clone()
		tvmakerclone1.Parent = game.Workspace
		second = true
	else
                --here needs to be the second clone 
		print("LOL")
	end
	
	wait(30)
	script.Parent.PurchaseScript.Disabled = false
	print("Script has been enabled!")

end)

Cause that does’t works either! Any other suggestions?

Do I have to put this inside the function?

Yes
Also, you should use a script instead of local script.

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.PurchaseScript.Disabled = true
	Cash.Value = Cash.Value - 500
	script.Parent.Parent.Visible = false

	wait(2.5)
	
	if not game.Workspace:FindFirstChild("TvClone1") then
        local tvmakerclone = game.ReplicatedStorage.TvMakers.TvMakerLVL1:Clone()
        tvmakerclone.Name = "TvClone1"
	    tvmakerclone.Parent = game.Workspace
    elseif game.Workspace:FindFirstChild("TvClone1") and not 
    game.Workspace:FindFirstChild("TvClone2") then
        local tvmakerclone = game.ReplicatedStorage.TvMakers.TvMakerLVL1:Clone()
        tvmakerclone.Name = "TvClone2"
	    tvmakerclone.Parent = game.Workspace
    end
end

Alright so I tested it in a local script then the first model gets cloned but… the other one not
And if I do it in a normal scipt it gives an error;

Change local Cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
By that

local Leader = player:WaitForChild("leaderstats" ,5)
if leader ~= nil then
    local Cash = Leader:WaitForChild("Cash" ,5)
    if Cash ~= nil then
        Cash.Value = Cash.Value - 500
    end
end

But you should use a script for that, because in a local script, the clone will be cloned in the client side, and the cash will be changed in client side.
Everything in client side can’t do anything on the server side unless you use a remote event.
So here your cash will be not saved and if your clone need to do something in the server side, like attack mobs or other player, it will not work.

Like this;

local hover = game.SoundService.HoverSound

script.Parent.MouseEnter:Connect(function()
	hover:Play()
end)

local player = game.Players.LocalPlayer
local Leader = player:WaitForChild("leaderstats" ,5)
if leader ~= nil then
	local Cash = Leader:WaitForChild("Cash" ,5)
	if Cash ~= nil then
		Cash.Value = Cash.Value - 500
	end
end
local leaderstats = player.leaderstats

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.PurchaseScript.Disabled = true
	Cash.Value = Cash.Value - 500
	script.Parent.Parent.Visible = false

	wait(2.5)

	if not game.Workspace:FindFirstChild("TvClone1") then
		local tvmakerclone = game.ReplicatedStorage.TvMakers.TvMakerLVL1:Clone()
		tvmakerclone.Name = "TvClone1"
		tvmakerclone.Parent = game.Workspace
	elseif game.Workspace:FindFirstChild("TvClone1") and not 
		game.Workspace:FindFirstChild("TvClone2") then
		local tvmakerclone1 = game.ReplicatedStorage.TvMakers.TvMakerLVL11:Clone()
		tvmakerclone1.Name = "TvClone2"
		tvmakerclone1.Parent = game.Workspace
	end
end
script.Parent.MouseButton1Click:Connect(function()

    local player = game.Players.LocalPlayer
    local Leader = player:WaitForChild("leaderstats" ,5)
    if leader ~= nil then
        local Cash = Leader:WaitForChild("Cash" ,5)
	    if Cash ~= nil then
		    Cash.Value = Cash.Value - 500
	    end
    end

    script.Parent.PurchaseScript.Disabled = true
    script.Parent.Parent.Visible = false
    wait(2.5)
	if not game.Workspace:FindFirstChild("TvClone1") then
		local tvmakerclone = game.ReplicatedStorage.TvMakers.TvMakerLVL1:Clone()
		tvmakerclone.Name = "TvClone1"
		tvmakerclone.Parent = game.Workspace
	elseif game.Workspace:FindFirstChild("TvClone1") and not 
		game.Workspace:FindFirstChild("TvClone2") then
		local tvmakerclone1 = game.ReplicatedStorage.TvMakers.TvMakerLVL11:Clone()
		tvmakerclone1.Name = "TvClone2"
		tvmakerclone1.Parent = game.Workspace
	end
end)
1 Like

Thank you for your help! Everything works just how i want it to be :upside_down_face: :ok_hand:

1 Like