Hello, I made a teleportation script so when you touch a brick it will "teleport" you to a different place.
This is in LocalScript:
wait(1) player = game.Players.LocalPlayer button = script.Parent local debounce = false function teleport() if not debounce then debounce = true Torso = player.Character.Torso Torso.CFrame = game.Workspace.middletele.CFrame end end script.Parent.Touched:connect(teleport) while true do wait() debounce = false wait(1) end
However it wont work.. and I dont know why.. please help me its really important. Thank you.
The first and foremost being game.Players.LocalPlayer
. This is not possible in a Server Script
.
It also seems you are using a LocalScript
. You should not be using in this scenario unless you plan on using Remote Events
.
The second suggestion is that for this scenario, all of your variables should be localized
i.e local
You also do not need the while true do
loop here. Unless you are using for some other thing, in this case, it is merely pointless.
You also should be using HumanoidRootPart
because R15
does not support Torso
and you could run into an error if you do plan on using R15
. If you still would like to use torso, then you will use UpperTorso
and LowerTorso
.
You also should not be using deprecated events. Use Connect()
instead of connect()
.
As suggested by other user, you are going to be using a parameter in the function
called hit
. Although, the name can be anything.
Here is a fix to your code. Do keep in mind that this is a Server Script
.
-- [Declaration Section] local Part = script.Parent; local Debounce = false; -- [Processing Section] local function Teleport_Player (Hit) if Hit.Parent:FindFirstChild("Humanoid") then if not Debounce then Debounce = true; local HRP = Hit.Parent.HumanoidRootPart; HRP.CFrame = CFrame.new(10, 100, 10); Debounce = false; end; else print("Other Object found"); end; end; -- [Connecting Section] Part.Touched:Connect(Teleport_Player);
Local scripts have special properties that allow them to get the local player, local player's mouse, etc.
These properties don't work on the player when you don't run it on the player though.
The problem is where the local script is located.
button = script.Parent
I'm guessing because of this line that the local script is a descendant of workspace and cannot run on the player.
I suggest either
button = workspace.Button -- or where ever/ whatever it's called
or 2. Re-write the script so that it detects anyone who touches it and then teleport them (you would do this by getting the hit parameter through .Touched() by doing:)
function teleport(hit)
Hope this has helped you in anyway!