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

Why is 'player' read as a number?

local swords = game.ServerStorage.Swords
local swordschildren = swords:GetChildren()
local secondaries = {swordschildren}

function equip(player)
	local stats = player.stats
	local backpackitems = player.Backpack:GetChildren()
	local swords = game.ServerStorage.Swords
	for i,v in pairs(backpackitems) do
		if v == secondaries then
			v:Delete()
			local itemclone = swords[script.Parent.Parent.ItemName.Text]:Clone()
			itemclone = player.Backpack
		end
	end
end

script.Parent.MouseButton1Down:connect(equip)

ERROR: 21:38:40.568 - Players.Player1.PlayerGui.ScreenGui.Shop.Pages.Inventory.ItemDetails.Equip.Script:6: attempt to index local 'player' (a number value) 21:38:40.569 - Stack Begin 21:38:40.569 - Script 'Players.Player1.PlayerGui.ScreenGui.Shop.Pages.Inventory.ItemDetails.Equip.Script', Line 6

When I printed the number which it thought player was, it simply printed the number '1078'. I'm very confused and tired. Your help is much appreciated.

1 comment

1

Is this a LocalScript or a ServerScript? https://scriptinghelpers.org/blog/common-mistakes#GuiEvent

1 answer

The error says that player is a number, so you should believe that.

Where is player defined? On line 5, as the parameter to equip.

How does equip get called? By the MouseButton1Down event (of presumably a GuiButton).

Let's check MouseButton1Down on the Wiki.

The event says it passes two numbers, an x and a y, presumably the coordinates of the click.

Not the player who clicked.

Why would you expect it to be a Player object if the documentation doesn't suggest that?


GUI events like clicking should not be done in a Script by the server; they should be done in a LocalScript and communicated to the server via RemoteEvents.

This Wiki article walks through a very simple example (almost exactly what you are doing) of properly splitting your logic between LocalScripts and Scripts.

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