Script doubles destroy function

Hello Developers! I Have problem, when I buy Spawn teleport and later other Teleport e.g. Forest then script doubles destroying on Spawn or other Teleport

for i,v in pairs(script.Parent.TeleportBg.ScrollingFrame:GetChildren()) do
	v.MouseButton1Click:Connect(function()
		if v:FindFirstChild("Locked") and v.Name ~= "VIP" and plr.leader.Areas.Value >= v.AreaReq.Value then
			script.Parent.StepTwo.Visible = true
			script.Parent.TeleportBg.Visible = false
			script.Parent.StepTwo.Price.Value = v.Price.Text
			script.Parent.StepTwo.World.Value = v.Name
			script.Parent.StepTwo.Info.Text = "Do you want to buy "..script.Parent.StepTwo.World.Value.." for "..script.Parent.StepTwo.Price.Value.." Gems?"
			script.Parent.StepTwo.YES.MouseButton1Click:Connect(function()
				if plr.leaderstats.Gems.Value >= script.Parent.StepTwo.Price.Value then
					game.ReplicatedStorage.BuyTeleport:FireServer(script.Parent.StepTwo.Price.Value, v)
					v.Locked:Destroy() -- Script doubles destroying on Spawn or other teleport when i buys second teleport 
					script.Parent.StepTwo.Visible = false
				end
			end)
			script.Parent.StepTwo.NO.MouseButton1Click:Connect(function()
				script.Parent.StepTwo.Visible = false
			end)
		elseif not v:FindFirstChild("Locked") and not v.Name ~= "VIP" and plr.leader.Areas.Value >= v.AreaReq.Value then
			script.Parent.TeleportBg.Visible = false
			local debounce = false
			local teleport = game.Workspace.Teleport:GetChildren()
			for i, tpname in pairs(teleport) do
				if tpname.Name == v.Name then
					if not debounce then
						debounce = true
						local LowerTorso = plr.Character.LowerTorso
						LowerTorso.CFrame = tpname.CFrame
					end
				end
			end
			wait(3)
			debounce = false
		end
	end)
end

Example
Someone: Buys Desert Teleport
Script: Destroys lock on Desert GUI Button
Somene: Buys Second Teleport e.g. Spawn
Script: Destroys lock on Desert again (Error) and Destroys lock on Spawn GUI Button

1 Like

Can you explain what and not v.Name ~= "VIP" does ?

2 Likes

VIP TextButton don’t have image named “Locked” so

if v:FindFirstChild("Locked") and v.Name ~= "VIP" and plr.leader.Areas.Value >= v.AreaReq.Value then

script works for all teleport except VIP

1 Like

Wait in your code you had a not infront of v.Name, did you remove it? Also on the elseif statement, what is the second check supposed to do?
This line: elseif not v:FindFirstChild("Locked") and not v.Name ~= "VIP" and plr.leader.Areas.Value >= v.AreaReq.Value then

2 Likes

its for teleporting plr to part but i have problem with buying teleport

Here is Example:
Me: Buy a Spawn Teleport
Script: Destroys “Locked” in Spawn TextButton
Me: Buy another teleport e.g. Desert
Script: Destroys “Locked” in Spawn TextButton (But script destroyed it earlier) and later Destroys “Locked” in Desert TextButton and i have error in Output

Locked is not a valid member of TextButton "{0A458A22-C905-4FEB-8287-040ACC2D0921}.R05HX.PlayerGui.Main.TeleportBg.ScrollingFrame.Spawn"
1 Like

What line

2 Likes

This line

v.Locked:Destroy()

here how it looks
https://www.youtube.com/watch?v=bU4OYtOVlGM

1 Like

You never disconnected the Yes and No buttons so they are being fired more than once.

for i,v in pairs(script.Parent.TeleportBg.ScrollingFrame:GetChildren()) do
	v.MouseButton1Click:Connect(function()
		if v:FindFirstChild("Locked") and v.Name ~= "VIP" and plr.leader.Areas.Value >= v.AreaReq.Value then
			script.Parent.StepTwo.Visible = true
			script.Parent.TeleportBg.Visible = false
			script.Parent.StepTwo.Price.Value = v.Price.Text
			script.Parent.StepTwo.World.Value = v.Name
			script.Parent.StepTwo.Info.Text = "Do you want to buy "..script.Parent.StepTwo.World.Value.." for "..script.Parent.StepTwo.Price.Value.." Gems?"
			local YesConnection
			local NoConnection
			YesConnection = script.Parent.StepTwo.YES.MouseButton1Click:Connect(function()
				if plr.leaderstats.Gems.Value >= script.Parent.StepTwo.Price.Value then
					game.ReplicatedStorage.BuyTeleport:FireServer(script.Parent.StepTwo.Price.Value, v)
					v.Locked:Destroy() -- Script doubles destroying on Spawn or other teleport when i buys second teleport 
					script.Parent.StepTwo.Visible = false
					YesConnection:Disconnect()
					NoConnection:Disconnect()
				end
			end)
			NoConnection = script.Parent.StepTwo.NO.MouseButton1Click:Connect(function()
				script.Parent.StepTwo.Visible = false
				YesConnection:Disconnect()
				NoConnection:Disconnect()
			end)
		elseif not v:FindFirstChild("Locked") and not v.Name ~= "VIP" and plr.leader.Areas.Value >= v.AreaReq.Value then
			script.Parent.TeleportBg.Visible = false
			local debounce = false
			local teleport = game.Workspace.Teleport:GetChildren()
			for i, tpname in pairs(teleport) do
				if tpname.Name == v.Name then
					if not debounce then
						debounce = true
						local LowerTorso = plr.Character.LowerTorso
						LowerTorso.CFrame = tpname.CFrame
					end
				end
			end
			wait(3)
			debounce = false
		end
	end)
1 Like