Player dies when teleported

My first scripting commission!

So I am making click to buy portal, and then when you click it again you will be teleported to a part. Script1:

local CD = script.Parent.ClickDetector
local datastore = game:GetService("DataStoreService") -- just realized this and the other below is not needed
local ds1 = datastore:GetDataStore("GemSaveSystem")

game.Players.PlayerAdded:Connect(function(plyr)
	wait(CD.MouseClick)
	CD.MouseClick:Connect(function(player)
		if plyr.leaderstats.Gems.Value >= 10 then -- replace 0 with how much it costs.
			plyr.leaderstats.Gems.Value -= 10 -- replace with what you want
			print("purchased")
			script.Parent.Purchased.Value = true
			script.Disabled = true
			return
		end
	end)
end)

Script 2:

local TP = game.Workspace.TeleportTo

while wait() do
	if script.Parent.Purchased.Value == true then
		script.Parent.ClickDetector.MouseClick:Connect(function(player)
			player.Character:MoveTo(TP.Position)
		end)
	end
end

The issue is that when the player teleports, they are under the map and killed. The block is can-collide off and above the ground.

A quick google search shows you should change the CFrame of the HumanoidRootPart, and also add a few studs to the Y axis of the teleport location.

It’s important that you show the values of variables that are defined somewhere else, and also stating where the scripts are.
Also, your scripts are pretty unoptimised. You should never use a while wait() do loop to wait for a value to change, always use the Object.Changed() signal.

Did you forget to Anchor the block

2 Likes

I’m just stupid. I’m done.
chars cause

You probably shouldn’t be defining Cd.MouseClick every 2 frames

1 Like

I agree, OP’s Script 2 is very unoptimized

should consider

local TP = game.Workspace.TeleportTo

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	if script.Parent.Purchased.Value == true then
		player.Character:MoveTo(TP.Position)
	end
end)
1 Like

that is the same script.
charsss

without the “while” connect, with a while connect, you are connecting many times that one event can trigger thousands of connected functions, which guessing how it would seem, it would look like the player gets a teleport stutter.

Not to mention that the first click detector will only work once