Script can't seem to find player names, help appreciated

Making a game that includes the functionality of one player challenging another. For this to work, a GUI button is clicked after the name of the player to be challenged is entered into a text box. I then need the script to find the player’s name as a child of “Players” to ensure that it exists. Problem is, the script cant seem to find the player name, despite being told where to find it. This results in an error stating: "attempt to index nil with ‘Name’.

I’m new to scripting entirely, so help will be greatly appreciated.

local Player = game:GetService("Players")
local LocalPlayer = Player.LocalPlayer

local playerName = LocalPlayer.Name -- Script fails and stops here.

game.Players:WaitForChild(playerName)

local fightButton = LocalPlayer.PlayerGui.ScreenGui.ChallengeUI.FightButton
local nameTextBox = LocalPlayer.PlayerGui.ScreenGui.ChallengeUI.NameBox
local reasonTextBox = LocalPlayer.PlayerGui.ScreenGui.ChallengeUI.ReasonBox

local announceBoxServer = LocalPlayer.PlayerGui.ScreenGui.ChallengeAnnounceServer
local announceBoxPlayer = LocalPlayer.PlayerGui.ScreenGui.ChallengeAnnouncePlayer

fightButton.MouseButton1Down:Connect(function()
	local name = nameTextBox.Text
	local reason = reasonTextBox.Text
	
	if name == game.Players:FindFirstChild(name) then
		print("Found the player to be challenged.")
	end
end)

Where’s this script located? can you ellaborate more?

1 Like

Sure. The script shown is located in ServerScriptService, as this will be the game’s main script. For challenging to work, the script needs the names of all objects that are a child of game.Players (which should just be players).

However, the script for some reason fails to find any names when playtesting, despite my character being loaded in and my player being a child of game.Players.

So the script is supposed to get the players name after clicking a gui button?

Only LocalScripts can access the local player, and they cannot run inside ServerScriptService. Try moving your code into a LocalScript and place it in one of these locations. I recommend placing the script in the same location as your GUI to avoid accessing the GUI through PlayerGui. Use RemoteEvents to send data to the server

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.