Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Problem with my buying script?

Whenever I click "yes" to purchase the sword, it charges me twice and it gives me 2 swords. I'm not sure why it does this, but I also get blue underline where it says "yesConnect:disconnect", on line 28.

price = 10
plr = game.Players.LocalPlayer
isowned = false

script.Parent.MouseButton1Down:connect(function()
    local KOs = plr.leaderstats:WaitForChild("KOs")
    if KOs.Value > price and isowned == false then
	    script.Parent.Parent.Visible = false
	    script.Parent.Parent.Parent.AreYouSure.Visible = true
	    local yesConnect
	    yesConnect = script.Parent.Parent.Parent.AreYouSure.Yes.MouseButton1Down:connect(function()
			isowned = true
			KOs.Value = KOs.Value - price
			local sworda = game.ReplicatedStorage.NoobSword:Clone()
			sworda.Parent = plr.Backpack
			local swordb = game.ReplicatedStorage.NoobSword:Clone()
			swordb.Parent = plr.StarterGear
			script.Parent.Parent.Visible = true
			script.Parent.Parent.Parent.AreYouSure.Visible = false
		end)
		elseif isowned == true then
			print('already owned')
		end
        local noConnect
        noConnect = script.Parent.Parent.Parent.AreYouSure.No.MouseButton1Down:connect(function()
        script.Parent.Parent.Visible = true
        script.Parent.Parent.Parent.AreYouSure.Visible = false
		yesConnect:disconnect()
        noConnect:disconnect()
    end)
end)

2 answers

✓ Accepted answer

I'm not sure why it's giving you two swords each time, but I moved the noConnect function inside the if statement, and made the yesConnect function disconnect both events:

price = 10
plr = game.Players.LocalPlayer
isowned = false

script.Parent.MouseButton1Down:connect(function()
    local KOs = plr.leaderstats:WaitForChild("KOs")
    if KOs.Value > price and isowned == false then
        script.Parent.Parent.Visible = false
        script.Parent.Parent.Parent.AreYouSure.Visible = true
        local yesConnect, noConnect
        yesConnect = script.Parent.Parent.Parent.AreYouSure.Yes.MouseButton1Down:connect(function()
            isowned = true
            KOs.Value = KOs.Value - price
            local sworda = game.ReplicatedStorage.NoobSword:Clone()
            sworda.Parent = plr.Backpack
            local swordb = game.ReplicatedStorage.NoobSword:Clone()
            swordb.Parent = plr.StarterGear
            script.Parent.Parent.Visible = true
            script.Parent.Parent.Parent.AreYouSure.Visible = false
		yesConnect:disconnect()
		noConnect:disconnect()
        end)
	local noConnect = script.Parent.Parent.Parent.AreYouSure.No.MouseButton1Down:connect(function()
        	script.Parent.Parent.Visible = true
        	script.Parent.Parent.Parent.AreYouSure.Visible = false
        	yesConnect:disconnect()
        	noConnect:disconnect()
    end)
    elseif isowned == true then
        print('already owned')
    end
end)

I think your problem is that your local variables are in a function. Move them out of there and it should work ^.^

local yesConnect
local yesConnect
local noConnect
price = 10
plr = game.Players.LocalPlayer
isowned = false

script.Parent.MouseButton1Down:connect(function()
    local KOs = plr.leaderstats:WaitForChild("KOs")
    if KOs.Value > price and isowned == false then
        script.Parent.Parent.Visible = false
        script.Parent.Parent.Parent.AreYouSure.Visible = true
        yesConnect = script.Parent.Parent.Parent.AreYouSure.Yes.MouseButton1Down:connect(function()
            isowned = true
            KOs.Value = KOs.Value - price
            local sworda = game.ReplicatedStorage.NoobSword:Clone()
            sworda.Parent = plr.Backpack
            local swordb = game.ReplicatedStorage.NoobSword:Clone()
            swordb.Parent = plr.StarterGear
            script.Parent.Parent.Visible = true
            script.Parent.Parent.Parent.AreYouSure.Visible = false
        end)
        elseif isowned == true then
            print('already owned')
        end
        noConnect = script.Parent.Parent.Parent.AreYouSure.No.MouseButton1Down:connect(function()
        script.Parent.Parent.Visible = true
        script.Parent.Parent.Parent.AreYouSure.Visible = false
        yesConnect:disconnect()
        noConnect:disconnect()
    end)
end)

4 comments

0

I had it that way because I need it my backpack and in startergear so you keep it on respawn.

0

It still charges twice, and I don't get the sword after buying now.

0

ok I will edit

0

there we go!

This discussion is preserved as it was. New answers, comments, and votes are closed.