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

Need help fixing this script and I really am not sure why its not working. help?

Hi again. Earlier I asked how do I make this teleport script E to activate instead of mouse button 1. I got some help but as a beginner scripter and a lack of understanding on my part I failed to make the script work...nevertheless here is my attempt. would love some correction.

local UserInputService = game:GetService("UserInputService")

local plr = game.Players.LocalPlayer

local teleports = workspace:WaitForChild("Teleports")
local enterButton = script.Parent.enterButton

local range = 5
local exitPart

UserInputService.InputBegan:Connect(function(GameProcessed, InputType)
    if not (GameProcessed) then return end

    if (InputType == Enum.KeyCode.E) then
	
	if exitPart then
		
		local hrp = plr.Character:FindFirstChild("HumanoidRootPart")
		
		if hrp then 
			
			hrp.CFrame = exitPart.CFrame + Vector3.new(0,3,0)
		end
	end
end
end)

game:GetService("RunService").RenderStepped:connect(function()
	
	for _,door in pairs(teleports:GetChildren()) do
		
		if door.Name:sub(0,4) == "Door" then 
			
			local exit = teleports:FindFirstChild("Exit"..door.Name:sub(5))
			
			local hrp = plr.Character:FindFirstChild("HumanoidRootPart")
			
			if hrp then 
				
				if (door.Position-hrp.Position).magnitude < range  then
					
					if not enterButton.Visible then
						enterButton.Visible = true
						
					end
					
				else
					
					if enterButton.Visible then enterButton.Visible = false end
					exitPart = exit
				end
			end
		end
	end
end

1 answer

✓ Accepted answer

The User input arguments were swapped the wrong way and being used incorrectly for KeyCode. You also don't need RenderStepped unless you're working with code that actually needs to run at that speed, like the camera.

local UserInputService = game:GetService("UserInputService")

local plr = game.Players.LocalPlayer

local teleports = workspace:WaitForChild("Teleports")
local enterButton = script.Parent.enterButton

local range = 5
local exitPart

UserInputService.InputBegan:Connect(function(input)
	if (input.KeyCode == Enum.KeyCode.E) then
		if exitPart then
			plr.Character:SetPrimaryPartCFrame ( exitPart.CFrame + Vector3.new(0,3,0) )
		end
	end
end)

while true do 
	for _, door in pairs(teleports:GetChildren()) do
		if door.Name:sub(0,4) == "Door" then
			local exit = teleports:FindFirstChild("Exit"..door.Name:sub(5))
			local PrimaryPart = plr.Character.PrimaryPart
			if PrimaryPart then 
				if (door.Position-PrimaryPart.Position).magnitude < range  then
					if not enterButton.Visible then
						enterButton.Visible = true
					end
					exitPart = nil
				else
					if enterButton.Visible then 
						enterButton.Visible = false 
					end
					exitPart = exit
				end
			end
		end
	end
	wait()
end

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