Problem with Teleportation

Okay so, im working on this custom admin system and im working on a teleportation command rn. Somehow the system doesnt work at some point on. Its hard to explain, but in one part of the code the variable displays the senders name and in the next part it just suddenly returns nil as if the value vanished. (I have marked these spots)

local function findplayer(name)
	for i, player in pairs(game.Players:GetPlayers())do
		if string.lower(player.Name) == name then
			return player 
		end
	end
	 return nil
end

commands.tphere = function(sender, arguments)
	local playertpto = sender
	print(sender)
	local playertotp = arguments[1]
	if playertotp and playertpto then
		local plrtotp = findplayer(playertotp)
		local plrtpto = findplayer(playertpto)
----------- the variable displays the senders name
		if playertotp and playertpto then
----------- the variable displays nil
			print(plrtotp)
			print(plrtpto)
			plrtotp.Character.HumanoidRootPart.CFrame = plrtpto.Character.HumanoidRootPart.CFrame
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message, receipient)
		if isAdmin(player)then
		message = string.lower(message)
			local splitstring = message:split(" ")
			local commandtriger = splitstring[1]
			local cmd = commandtriger:split(prefix)
			local cmdname = cmd[2]
			if commands[cmdname] then
				local arguments = {}
				for i = 2, #splitstring, 1 do
						table.insert(arguments,splitstring[i])
				end
				commands[cmdname](player, arguments)
			end	
		end
	end)
end)

You need to do string.lower(name) you’re compared a lowered string to a normal cased string.

local function findplayer(name)
	for i, player in pairs(game.Players:GetPlayers())do
		if string.lower(player.Name) == string.lower(name) then
			return player 
		end
	end
	 return nil
end

hmm, maybe you are checking the wrong variables, you’ve checked

if playertotp and playertpto then

twice

After doing the changes i got this error

That’s good so you know that name is a player instance so change it to name.Name in the string.lower and boom!

after changing it to if string.lower(player.Name) == string.lower(name.Name) then it throws this error at my head

I figured out what was wrong. The first time you run this function you send a string and the second time you send a player instance. This should account for both situations and make it dynamic hope this helped. :slight_smile:

local function findplayer(name)
	for i, player in pairs(game.Players:GetPlayers())do
		if (type(name) == "string") then
			if string.match(player.Name:lower(), name:lower()) then
				return player 
			end
		else
			if (player.Name == name.Name) then
				return player
			end
		end
	end
	 return nil
end
1 Like

Alright, now it worked. Huge thanks for helping!