The title basically explains what I need help doing, but for more detail, I need to make a player teleport while standing on a part when a key on their keyboard is pressed.
Code:
Player.Character.LeftFoot.Touched:Connect(function(Hit)
Mouse.KeyDown:Connect(function(Key)
local Pressed = false
if string.lower(Key) == "y" and Pressed == false then
Pressed = true
if Hit.Name == "TeleportPart" then
local ToPart = Hit.Parent.TeleportToPart
Player.Character.LowerTorso.CFrame = CFrame.new(Vector3.new(ToPart.Position.X, ToPart.Position.Y + 2, ToPart.Position.Z))
Pressed = false
-- More fancy stuff goes here but ya know
end
end
end)
end)
One of the problems is that since I end up setting Pressed to false that I can press Y again and it triggers the teleport to keep happening even when I am not touching “TeleportPart,” but I do not know how to make it so I have to be on it to trigger.
Try raycasting when you press Y to detect a part under you?
local uis = Game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycasyFilterType.Blacklist
rayParams.FilterDescendantsInstances = {char}
uis.InputBegan:Connect(function(input, processed)
if processed then return end
if input.KeyCode == Enum.KeyCode.Y then
local ray = workspace:Raycast(char.HumanoidRootPart.Position,char.HumanoidRootPart.CFrame.UpVector*-10,rayParams)
if ray then
local part = ray.Instance
if part.Name == "TeleportPart" then
local toPart = part.Parent.TeleportToPart
char:SetPrimaryPartCFrame(toPart.CFrame * CFrame.new(0,2,0))
end
end
end
end)
local teleportPart = ----your part
local isTouching = false
teleportPart.Touch:connect(function(hit)
isTouching = true
end)
teleportPart.TouchEnded:Connect(function()
isTouching = false
end)
Mouse.KeyDown:Connect(function(Key)
local Pressed = false
if string.lower(Key) == "y" and Pressed == false then
Pressed = true
if IsTouching then ----more simple
local ToPart = Hit.Parent.TeleportToPart --maybe change this
Player.Character.LowerTorso.CFrame = CFrame.new(Vector3.new(ToPart.Position.X, ToPart.Position.Y + 2, ToPart.Position.Z))
Pressed = false
-- More fancy stuff goes here but ya know
end
end
end)
workspace.TeleportPart.Touched:Connect(function(Hit)
Mouse.KeyDown:Connect(function(Key)
local Pressed = false
if string.lower(Key) == "y" and Pressed == false then
Pressed = true
if Hit.parent == player.Character then
local ToPart = workspace.TeleportToPart
Player.Character.LowerTorso.CFrame = CFrame.new(Vector3.new(ToPart.Position.X, ToPart.Position.Y + 2, ToPart.Position.Z))
Pressed = false
-- More fancy stuff goes here but ya know
else
pressed = false
end
end
end)
end)