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)
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)
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.
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.