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)