Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Problem with my buying script?

Asked by
Relatch 550 Moderation Voter
9 years ago

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

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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)
Ad
Log in to vote
1
Answered by
Hero_ic 502 Moderation Voter
9 years ago

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)

0
I had it that way because I need it my backpack and in startergear so you keep it on respawn. Relatch 550 — 9y
0
It still charges twice, and I don't get the sword after buying now. Relatch 550 — 9y
0
ok I will edit Hero_ic 502 — 9y
0
there we go! Hero_ic 502 — 9y

Answer this question