Attempt to call a nil value

I get an error when I try to use this script. It’s a teleport script which lets a player teleport another player in front of them. It works fine until I try to rank lock it to my group…
I check if my rank is higher than my target’s rank, if it is, they teleport, if not they don’t.

Server script:

game.ReplicatedStorage.AbilityEvents.Summon.OnServerEvent:Connect(function(pass,Player,targetPlayer)
	if Player:GetRankInGroup(11954854) >= targetPlayer:GetRankInGroup(11954854) then
	local Plr = game.Players:FindFirstChild(Player)
	print(Plr.Name)
	print(Plr.Name)
	local TargetPlr = game.Players:FindFirstChild(targetPlayer)
	TargetPlr.Character.HumanoidRootPart.CFrame = Plr.Character.HumanoidRootPart.CFrame * CFrame.new(0, 0, -5) * CFrame.Angles(0, 100000, 0)
	else
		game.ReplicatedStorage.Message:FireClient(Player, "You cannot summon this player!")
		end
end)

local script

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Mouse = Player:GetMouse()
local UserInputService = game:GetService("UserInputService")

repeat
	wait()
until Character.Parent == workspace

local Debounce = false

Player.Chatted:Connect(function(Message)
	local lower = string.lower(Message)
	local Object = nil
	local targetPlayer = nil
	print(string.sub(lower, 1, 9))
	if string.sub(lower, 1, 9) == "i summon," and not Debounce then
		Debounce = true
		local Target = string.sub(lower, 11, #lower)
		print(Target)
		for _,v in next, Players:GetPlayers() do
			if string.sub(string.lower(v.Name), 1, #Target) == Target then
				Object = v.Character.Humanoid
				targetPlayer = v
			end
		end
		if not Object then
			for _,v in next, workspace:GetDescendants() do
				if v.Name:lower() == Target and v:IsA("Model") then
					Object = v
				end
			end
		end
		if targetPlayer then
			game.ReplicatedStorage.AbilityEvents.Summon:FireServer(Player.Name,targetPlayer.Name)
			wait(5)
			Debounce = false
			return
		end
	end
end)

Error is on line 2 of the server script, which is

if Player:GetRankInGroup(11954854) >= targetPlayer:GetRankInGroup(11954854) then

You are passing player names so you need to find your players before running player methods.

game.ReplicatedStorage.AbilityEvents.Summon.OnServerEvent:Connect(function(pass,Player,targetPlayer)
	local Plr = game.Players:FindFirstChild(Player)
	local TargetPlr = game.Players:FindFirstChild(targetPlayer)
	if Player:GetRankInGroup(11954854) >= targetPlayer:GetRankInGroup(11954854) then

You are doing this

game.ReplicatedStorage.AbilityEvents.Summon:FireServer(Player.Name,targetPlayer.Name)

Sending player name, which wont work, you can do

game.Players:FindFirstChild(name)

Or you can try to pass the UserId and get the player by

game.Players:GetPlayerByUserIdAsync(id) --or something like this

According to your error Player or targetPlayer is ‘nil’.

1 Like

this didn’t work unfortunately.

that line is not the error.
the error is line 2.

Actually, the error means that GetRankInGroup is nil. It looks to me like that is because targetPlayer or Player is a string.
("testString").nilFunction() will produce the same error.

If targetPlayer or Player were nil, it would produce this error instead:
Attempt to index nil with 'GetRankInGroup'

1 Like

They didn’t provide the stack trace.

Error is on line 2 of the server script, which is

if Player:GetRankInGroup(11954854) >= targetPlayer:GetRankInGroup(11954854) then

I hadn’t read the thread title before so I was under the assumption that an error was caused due to either of the values referring to ‘nil’ (which happens to be the case most of the time).

("testString").nilFunction()

Would produce an error with the same message but the semantics of the error are different. In that example the function is being called as a function (via the dot operator) as opposed to as a method (via the colon operator).

("Hello world!").sub(1, 5) --Invalid because the string is treated as a blank table (no metatable).
("Hello world!"):sub(1, 5) --Valid because the string is treated as an object (metatable exists).
("Hello world!").fake() --This would attempt to index a key named 'fake' inside the string value directly.
("Hello world!"):fake() --This would invoke the string library's metatable which points to the string library to look for a key named 'fake'.

I don’t think it actually makes a difference, and the colon operator isn’t necessarily correlated with metatables.
image

I don’t think it actually makes a difference

A semantical difference is simply a difference in meaning (the reason behind the error is different).

and the colon operator isn’t necessarily correlated with metatables.

More often than not it is, and for the discussed example (string values), it is.

This is the reason the error is occurring, just do what i said and it will work

The first parameter of the onserver event is the player that fires that event so try this:

game.ReplicatedStorage.AbilityEvents.Summon.OnServerEvent:Connect(function(Player1, pass,Player,targetPlayer)

You’re passing the “Player” and “targetPlayer” arguments as strings from the local script rather then their instances, you should just remove the “.Name” from both arguments here: game.ReplicatedStorage.AbilityEvents.Summon:FireServer(Player.Name,targetPlayer.Name)
also you probably shouldn’t have the client tell you who the Player is cause the client could easily tp other people to eachother with exploits, instead move Player to the first argument and don’t pass it from the local script so you can get the actual player who fired the event, just the targetPlayer argument:
game.ReplicatedStorage.AbilityEvents.Summon:FireServer(targetPlayer.Name)
game.ReplicatedStorage.AbilityEvents.Summon.OnServerEvent:Connect(function(Player,targetPlayer)