Attempt to index nil with 'Character

hey guys, im trying to get the player from the text on my ui and if it does then to kill the player. but if i select my own name i dont want the script to run. i appreciate any help ty!

script:

yes.MouseButton1Click:Connect(function()
	plrchoiceUI.Enabled = false
	if selectedButton then
		selectedButton.BackgroundColor3 = unselectedColor
	end
	if selectedButton then
		local findPlr:Player = plrs:FindFirstChild(selectedButton.Text)
		if findPlr then
			plrcheckEvent:FireClient(findPlr)
		end
	end
end)

local script:

local plrcheckEvent = game.ReplicatedStorage:WaitForChild("plrcheckEvent")

plrcheckEvent.OnClientEvent:Connect(function(findplr)
	if 	findplr.Character.Humanoid.Health > 0 then
		findplr.Character.Humanoid.Health = 0
	end
end)

dc7a8a64f8f7b8068301e0434cf3dd7d

1 Like

Are you trying to get the name from the confirmation GUI or the players list?

2 Likes

i saw ur other reply on my other post, thank u so much for the info! i really appreciate it. :slight_smile:

so basically, i only want to kill other players. if i select my own player i just want it to do nothing (return).

hopefully that makes sense.

this works and kills other players but i dont want it to kill my own if i select it:

yes.MouseButton1Click:Connect(function()
	plrchoiceUI.Enabled = false
	if selectedButton then
		selectedButton.BackgroundColor3 = unselectedColor
	end
	if selectedButton then
		local findPlr:Player = plrs:FindFirstChild(selectedButton.Text)
		if findPlr then
			if findPlr.Character.Humanoid.Health > 0 then
				findPlr.Character.Humanoid.Health = 0
			end
		end
	end
end)
1 Like

You could just make your name not appear in the first place.

Or, just do something like:

  • have a table of names to ignore
  • then, do

if table.find(ignorePlayers, findPlr.Name) then return nil end

1 Like

yes thats a great idea tbh i might try smth like that out.

i was thinking if i could get the players name from the yes.MouseButton1Click:Connect(function()

and if its the same as the name from the list then to return. u wouldnt know how to do that would u?

there was this post i found:

I’m actually curious. Why are you detecting the click on the server and then firing a remote event to the client? It should be the other way around. Doing that will allow you to just use:

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

and compare it to the findPlr variable.

2 Likes

my lordy, several things are funky bro.
Legit things that are supposed to be on local is on the server, and things that are on the server are on the local??
broski–huh

You shouldn’t be doing this on the local

Server:

-- Server script
local plrcheckEvent = game.ReplicatedStorage:WaitForChild("plrcheckEvent")

plrcheckEvent.OnServerEvent:Connect(function(player, findplr)
	if findplr and findplr ~= player and findplr.Character and findplr.Character:FindFirstChild("Humanoid") then
		findplr.Character.Humanoid.Health = 0
	end
end)

Local:

-- Local script
yes.MouseButton1Click:Connect(function()
	plrchoiceUI.Enabled = false
	if selectedButton then
		selectedButton.BackgroundColor3 = unselectedColor
	end
	if selectedButton then
		local findPlr = plrs:FindFirstChild(selectedButton.Text)
		if findPlr and findPlr ~= game.Players.LocalPlayer then
			plrcheckEvent:FireServer(findPlr)
		end
	end
end)

banner

2 Likes

yes u are indeed right. i suppose its my chaotic inexperience really. i tried to get my player from a local script. ahh long story, im just chaotic LOL.

this is what i tried:

local:

local getPlr = game.ReplicatedStorage:WaitForChild("getPlr")
local myPlr = game.Players.LocalPlayer

getPlr:FireServer(myPlr)

script:

yes.MouseButton1Click:Connect(function()
	plrchoiceUI.Enabled = false
	if selectedButton then
		selectedButton.BackgroundColor3 = unselectedColor
	end
	if selectedButton then
		local findPlr:Player = plrs:FindFirstChild(selectedButton.Text)
		if findPlr then
			
			getPlr.OnServerEvent:Connect(function(myPlayer)
				if myPlayer.Name == findPlr then
					print("player name is = to player name in list, returning")
				end
				
				if findPlr.Character.Humanoid.Health > 0 then
					findPlr.Character.Humanoid.Health = 0
				end
			end)
		end
	end
end)

The first parameter of OnServerEvent is always the player, while you’re typing it, it gives a tooltip above your cursor.


So 1 you don’t need to pass in myPlayer
And 2

You’re setting the myPlayer to the player parameter, so it would actually be (plr, myPlayer) to actually get the variable.

as for this:

if you’re dead-set on doing it that way, change findPlr to findPlr.Name
and don’t forget to add return in the if condition, it’ll keep on running if you left it like that.

banner

1 Like

bro im telling u. im all over the place rofl :sob: . i like the gif btw.

so i took a lot of ur advice and its defo optimized better but its still killing my player . my selectedbutton is on the server script and everything is messed up pretty much but this is what i tried:

local script:

local plrchoiceUI = script.Parent.plrChoiceUI
local plrcheckEvent = game.ReplicatedStorage:WaitForChild("plrcheckEvent")
local yes = plrchoiceUI.Frame.yes
local myPlr = game.Players.LocalPlayer

	yes.MouseButton1Click:Connect(function()
		plrchoiceUI.Enabled = false
				plrcheckEvent:FireServer(myPlr)
	end)

script:

plrcheckEvent.OnServerEvent:Connect(function(myplr)
	plrchoiceUI.Enabled = false
	if selectedButton then
		selectedButton.BackgroundColor3 = unselectedColor
	end
	if selectedButton then
		local findPlr = plrs:FindFirstChild(selectedButton.Text)
		if findPlr and findPlr ~= myplr.Name then
			if 	findPlr.Character.Humanoid.Health > 0 then
				findPlr.Character.Humanoid.Health = 0
			end
			end
		end
end)

EDIT: NVM it works. i had to do: findPlr ~= myplr.Name → findPlr ~= myplr. tysm for ur help man!!!

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