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

How do I define "player"?

I'm making a script, and in the output, the only error is that I haven't defined the player, its for a script to equip a tool in a GUI, for a inventory system I made, but the function is : "script.Parent.MouseButton1Click:connect(function(player)", but I need to define player to define the pickaxe. So how would I define the player, here is the code :

local pickaxe = player.Backpack:FindFirstChild("Pickaxe")
--------------------------- "player" definition needed here
script.Parent.MouseButton1Click:connect(function(player)
	local pickaxe = player.Backpack:findFirstChild("Pickaxe")
	if script.Parent.Text == "Unequip" then
		pickaxe.Parent = game.ServerStorage
		script.Parent.Text = "Equip"
	else
		pickaxe:Clone().Parent = player.Backpack
		script.Parent.Text = "Unequip"
	end
end)

2 answers

If your code is in a local script you can define player with the following code:

local player = game.Players.LocalPlayer

LocalPlayer is a property in game.Players that returns Player only if it is called in a localscript, calling in a regular script will result in error

Tips on localscripts

  • localscripts should only be used inside StarterGui or StarterPack or anywhere thats within the Player object. it can also be used in tools/hopperbins

2 comments

0

But what do I do if it ISN'T a local script. I even stated in my question that its a normal "script".

0

By the looks of it the script is under PlayerGui/StartGui just put the code in a localscript and use that

just say this in your script

game.Workspace.player

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