How do I fix this function?

Made this function to teleport/reward you, but the function keeps stalling.

is this a syntax error or did I place the “reward” to close to the teleport script?

local  Respawner = game.Workspace.respawner

script.Parent.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
		local Tele = Player.Character:FindFirstChild("Tele")
		if not Tele then return end
		
		if not Tele.Value then
			Tele.Value = true
			Player.Character.HumanoidRootPart.CFrame = Respawner.CFrame + Vector3.new(0, 5, 0)
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 85 
			wait(3)
			Tele.Value = false
		end
	end
end)

This would be better on #help-and-feedback:scripting-support.

Hello!

What exactly is the trouble in the script?
Could you send some output Screenshots of this script?

Well first of all your not checking if it’s a player touching so it could be breaking on the :GetPlayerFromCharacter function so instead do

script.Parent.Touched:Connect(function(hit)
  if hit and hit.Parent:FindFirstChild('Humanoid') then
    local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if Player then
      local Tele = Player.Character:FindFirstChild("Tele")
      if not Tele then return end

      if not Tele.Value then
			Tele.Value = true
			Player.Character.HumanoidRootPart.CFrame = Respawner.CFrame + Vector3.new(0, 5, 0)
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 85 
			wait(3)
			Tele.Value = false
       end
	end
end)

There seems to be a problem getting the teleporter to work, so it continues to break on line 9. there is really no output or error messages though?


It is talking about the look vector not being apart of the workspace.

it isn’t Part.lookVector it’s Part.CFrame.lookVector

This isnt the problem the problem is with spawning me to the “Respawner” part.

Player.Character.HumanoidRootPart.CFrame = Respawner.CFrame + Vector3.new(0, 5, 0)

local Respawner = workspace.respawner

local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
	local playerObject = Players:GetPlayerFromCharacter(hit.Parent)

	if playerObject then
		local character = playerObject.Character
		local tele = character:FindFirstChild("Tele")
		if not tele then return end

		if not tele.Value then
			tele.Value = true
			
			character:PivotTo(Respawner.CFrame + Vector3.new(0, 5, 0))
			playerObject.leaderstats.Coins.Value += 85

			task.wait(3)
			Tele.Value = false
		end
	end
end