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

How do fix and prevent this kind of player error in my GUI?

Asked by 8 years ago

Hello, I was making a script for a gui. The problem is the output says: 22:26:11.460 - Players.Player1.PlayerGui.BondsGui.Background.Paper.Enter.LocalScript:10: attempt to index local 'player' (a number value). How do I fix it?

local Contract = script.Parent.Parent.Parent.Parent
local CentralBank = game.Lighting.Bank
local InVal = script.Parent.Parent.InputVal.Value
local bondprice = game.Lighting.Bank.Price.BondPrice



function onClicked(player)
    local OutVal = script.Parent.Parent.OutputVal.Value
    local Money = player:WaitForChild("PlayerMoney")

    local AccountName = Instance.new("StringValue")
    AccountName.Parent = game.Lighting.Bank.BankAccounts
    AccountName.Name = player.Name


    local AccountType = Instance.new("StringValue")
    AccountType.Parent = AccountName
    AccountType.Name = "Bond"

    local Worth = Instance.new("NumberValue")
    Worth.Name = "Worth"
    Worth.Value = OutVal
    Worth.Parent = AccountName

    CentralBank.BankMoney.Value = CentralBank.BankMoney.Value + OutVal
    Money.Value = Money.Value - OutVal
    Contract:Destroy()

    wait()


end
script.Parent.MouseButton1Up:connect(onClicked)

3 answers

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago
local Contract = script.Parent.Parent.Parent.Parent
local CentralBank = game.Lighting.Bank
local InVal = script.Parent.Parent.InputVal.Value
local bondprice = game.Lighting.Bank.Price.BondPrice
local player = game.Players.LocalPlayer-----You did not define player


function onClicked(player)
    local OutVal = script.Parent.Parent.OutputVal.Value
    local Money = player:WaitForChild("PlayerMoney")

    local AccountName = Instance.new("StringValue")
    AccountName.Parent = game.Lighting.Bank.BankAccounts
    AccountName.Name = player.Name------Without player defined, it get's an error


    local AccountType = Instance.new("StringValue")
    AccountType.Parent = AccountName
    AccountType.Name = "Bond"

    local Worth = Instance.new("NumberValue")
    Worth.Name = "Worth"
    Worth.Value = OutVal
    Worth.Parent = AccountName

    CentralBank.BankMoney.Value = CentralBank.BankMoney.Value + OutVal
    Money.Value = Money.Value - OutVal
    Contract:Destroy()

    wait()


end
script.Parent.MouseButton1Up:connect(onClicked)

0
He defined the player in the argument of onClicked. Setting a variable won't do anything to help, the argument overwrites it anyways. He needs to remove the argument. Ethan_Waike 156 — 8y
Ad
Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

A MouseButton1Up event listener doesn't have the player as a parameter; it has x and y, being the coordinates at which the mouse button release occurred. You can get the player using game.Players.LocalPlayer:

local Contract = script.Parent.Parent.Parent.Parent
local CentralBank = game.Lighting.Bank
local InVal = script.Parent.Parent.InputVal.Value
local bondprice = game.Lighting.Bank.Price.BondPrice
local player = game.Players.LocalPlayer --// This will be the player which is affected by the LocalScript.


local function onClicked()
    local OutVal = script.Parent.Parent.OutputVal.Value
    local Money = player:WaitForChild("PlayerMoney")

    local AccountName = Instance.new("StringValue")
    AccountName.Parent = game.Lighting.Bank.BankAccounts
    AccountName.Name = player.Name


    local AccountType = Instance.new("StringValue")
    AccountType.Parent = AccountName
    AccountType.Name = "Bond"

    local Worth = Instance.new("NumberValue")
    Worth.Name = "Worth"
    Worth.Value = OutVal
    Worth.Parent = AccountName

    CentralBank.BankMoney.Value = CentralBank.BankMoney.Value + OutVal
    Money.Value = Money.Value - OutVal
    Contract:Destroy()

    wait()
end
script.Parent.MouseButton1Up:connect(onClicked)

Hope this helped.

0
dang @Pyrondon, you got it 38 secs ago... I got one min ago heh xXxDaBossElmoxXx 22 — 8y
0
You ninja'd me fair and square. Pyrondon 2089 — 8y
Log in to vote
0
Answered by 8 years ago

local Money = player:WaitForChild("PlayerMoney")

This, on line 10. You index the player as function onClicked(player), but the arguments of the MouseButton1Up function are MouseButton1Up ( int x, int y ).

If you want to get the player, you could do game.Players.LocalPlayer (assuming this is a local script, which I'm pretty sure it's not due to context).

Otherwise you can use the hierarchy to get the player.

player = script.Parent.Parent.Parent.Parent

assuming the scripts parent is a frame, the frames parent is a ScreenGui, the screengui's parent is the playergui, and the playergui's parent is the player itself. Manipulate that to get the player, because "Contract = script.Parent.Parent.Parent.Parent" contradicts what I just said.

Contract = script.Parent.Parent.Parent.Parent
CentralBank = game.Lighting.Bank
InVal = script.Parent.Parent.InputVal.Value
bondprice = game.Lighting.Bank.Price.BondPrice
player = nil --index the player here instead of in the onClicked()'s arguments.


function onClicked()
    local OutVal = script.Parent.Parent.OutputVal.Value
    local Money = player:WaitForChild("PlayerMoney")

    local AccountName = Instance.new("StringValue")
    AccountName.Parent = game.Lighting.Bank.BankAccounts
    AccountName.Name = player.Name


    local AccountType = Instance.new("StringValue")
    AccountType.Parent = AccountName
    AccountType.Name = "Bond"

    local Worth = Instance.new("NumberValue")
    Worth.Name = "Worth"
    Worth.Value = OutVal
    Worth.Parent = AccountName

    CentralBank.BankMoney.Value = CentralBank.BankMoney.Value + OutVal
    Money.Value = Money.Value - OutVal
    Contract:Destroy()

    wait()


end
script.Parent.MouseButton1Up:connect(onClicked)

Typed this fast, pardon any errors.

0
Actually excuse what I said about it not being a local script, forgot not everyone uses filtering enabled. Ethan_Waike 156 — 8y

Answer this question