How to get player and players backpack in a server script inside of a text button in StarterGui

Hello i am making a console for my game i have made a working version using a local script but i need it to use a server script instead so i can add developer commands. Like giving items to people or spawning in npc's or giving players currency. But in my script i cant get a reference to the player that clicked the textbutton to run the command that they type. When i click the run command buttton it does nothing and no errors saying player is nil or somthing.

heres the entire script. It's inside of a textbutton which is in a ScreenGui in StarterGui

script.parent.MouseButton1Click:Connect(function()
	game.Players.PlayerAdded:Connect(function(player)
		local backpack = player.Backpack
		
		repeat wait() until player.Character -- wait for character to load
		
		local torso = player.Character.Torso
			
		local input = script.Parent.Parent.input
		local output = script.Parent.Parent.OutputFrame.output
			
		local tool = game.ReplicatedStorage.Weapons.KeyCard1
		local RemoteEvent = script.Parent.RemoteEvent
	
		output.Text = input.Text -- set output.Text to whatever user types in input
		
		--|| USER COMMANDS ||--
		if input.Text == "!commands" then -- list available commands
			output.Text = [[
				  --PLAYER COMMANDS--				
				scl_Brightness=  <--number value 1-5
				scl_Inventory.Clear
			
				  --DEVELOPER COMMANDS--
				scl_giveKeyCard
			]]
		end
		
		if input.Text == "scl_Inventory.Clear" then -- drop items in backpack
			backpack:ClearAllChildren()
		end
		
		--| DEVELOPER COMMANDS |--
		if input.Text == "scl_giveKeyCard" and player.Name == "Cletdawg" or player.Name == "SpaceCitizenDev" then
			RemoteEvent:FireServer(tool)
			wait(0.2)
		end
	end)
end)

The first issue I see is that the PlayerAdded event will not connect until after the MouseButton1 event is fired, so it will miss out any players that join before the button is pressed.

Secondly, any events related to GUIObjects must be in a local script. If you need to communicate with the server, use a remote event to trigger a function in a server script, passing the command from the console on the client to the server.

1 Like

Should i remove the game.Players.PlayerAdded function? i only put that in as an example of what im trying to do. I dont understand lua and remote events very well so im still not sure what to do from here

Since this code should exist on the client, using the PlayerAdded event is unnecessary because you can access the local player with game.Players.LocalPlayer.

Remote events are critical in this instance as they allow the client to talk to the server. You would pass the command the player gives to the remote event, then a function on the server would parse that command and do stuff. Check out the examples and documentation at: RemoteEvent | Documentation - Roblox Creator Hub

1 Like

I think i understand what your saying actually . I wasn’t aware of how to use a remote event but my friend who helped me write some of this used a remote event to basically do what you just said except with a command that gives you a tool. So i should be able to use that to help me figure it out. Thanks

1 Like