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

What is wrong with this buy Gui?

Asked by 10 years ago

This is pretty simple stuff, but it still isn't working. When I click the button, it puts the error "Players.Player1.PlayerGui.ScreenGui.ShopBG.Sword1.BuyScript:7: attempt to index local 'Player' (a number value)", and then a few lines later "Disconnected event because of exception". Can someone tell me what's wrong?

price = 10
clone = game.Lighting.Sword1
Name = "Wood Sparring Sword"
Player = game.Players.LocalPlayer

function onClick(Player)
    if Player.leaderstats.Gold.Value >= price then
        Player.leaderstats.Gold.Value = Player.leaderstats.Gold.Value - price
        clone:clone()
        clone.Parent = Player.Backpack
        clone.Name = Name
    elseif Player.leaderstats.Gold.Value < price then
        print("Player does not have the required funds for "..Name)
    else

    end
end

script.Parent.MouseButton1Down:connect(onClick)

1 answer

Log in to vote
1
Answered by 10 years ago

well first off, for the function on line 6 you dont need Player as an argument, it might confuse things. secondly, one line 9 you have the sword clone, yet you dont have a variable set to it to do with as you want. Thirdly, on line 12, you do not need elseif to define parameters for something that would happen regardless, you have it >= price up top, so anything else would be <, so you dont have to have elseif.

if you were to rewrite the code to be more efficient, it would go along something like this:

price = 10
item = game.Lighting:WaitForChild("Sword1") -- make sure it loads
player = game.Players.LocalPlayer -- if its a local script

script.Parent.MouseButton1Down:connect(function()
local gold = player:WaitForChild("leaderstats"):WaitForChild("Gold")
if gold.Value >= price then
gold.Value = gold.Value - price
local copy = item:clone()
copy.Parent = player.Backpack
else
print(player.Name.." does not have enough gold for "..item.Name)
end
end)

this is a quick and easy remake, but I hope you can learn from the different mistakes you made and continue to grow in lua :) Just keep in mind, not all functions need arguments, and when you clone something, make sure you set a variable and its parent.

Ad

Answer this question