Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Invalid player to teleport?

Hello im a new scripter im still learning and im try to make a touch part teleport script for my game but when i run it and check the output it says invalid player i dont know how fix it

here code

local TeleportService = game:GetService("TeleportService")
local place = Removed place id

function onTouch(hit)
	local Players = game:GetAttributes(hit)

	TeleportService:Teleport(place, hit , Players)

end
script.Parent.Touched:connect(onTouch)

i really dont know how fix it i send help to my friend but he dont know too.

1 answer

✓ Accepted answer

Hi superplayer1453! I see what's wrong here!

You seems to take a player from a attribute, but the attribute doesn't exist, you cannot create a attribute holding a object-typed variable.

here my solution with comments

local TeleportService = game:GetService("TeleportService")
--we will need this service to get the player
local players = game:GetService("Players")

local place = Removed place id

function onTouch(hit)
	--the hit variable will return for exemple left leg, right leg or smth else,
	--so we check if for exemple left leg has a parent and if the parent has a humanoid
	--if it has a humanoid it means a player touched the part.
	if hit.Parent and hit.Parent:WaitForChild("Humanoid") then
		--this means the character model is the parent of left leg.
		local char = hit.Parent
		--Then we use a built-in function to get the player object by giving the char.
		local plr = players:GetPlayerFromCharacter(char)
		TeleportService:Teleport(place, plr)
	end
end
script.Parent.Touched:connect(onTouch)

2 comments

0

Oh wow thanks!

0

No problem !

This discussion is preserved as it was. New answers, comments, and votes are closed.