Problem with E to interact script

Well I am trying to make a E to interact script that will make the player’s camera focus be to a part, but it doesn’t appear to work, here is the script:

local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local humanoidroot = Char:WaitForChild("HumanoidRootPart")
local cam = workspace.CurrentCamera
local UIS = game:GetService("UserInputService")
local hitbox = game.Workspace.hitboxes.furniture_store
local distance = hitbox.distance.Value

UIS.InputBegan:Connect(function(keyCode)
	if keyCode == Enum.KeyCode.E then do
			if (hitbox.Position - humanoidroot.Position).magnitude < distance then do
					repeat 
        					cam.CameraType = Enum.CameraType.Scriptable
        				wait()
    						until cam.CameraType ==  Enum.CameraType.Scriptable

    					cam.CFrame = workspace.FurnitureStoreCam.CFrame
				end
			end
		end
	end
end)
1 Like

Perhaps consider changing it so repeat wait()

Also, there is not really a need for the then do, you could just use then

Ok, thanks for the tip but the error looks like this:

image

Notice on 2nd line into your InputBegan function, you don’t need do after then.
From what I’ve seen, then is used in conditional statements, whereas do is used in loops.
if is a conditional statement. So that line should only read:

if keyCode == Enum.KeyCode.E then

Are you using a LocalScript? You can’t call for the LocalPlayer if the script is running on the server-side.

If you call for the LocalPlayer using a server script, it will return nil, as the LocalPlayer does not exist. The LocalPlayer is the one running the script and the player that the script effects (Which is why different players can see different things at one given time)

I’d recomend using ContextActionService for doing keybinds and stuff
also you don’t need to repeatedly change the cameratype to what you want until it’s what you want
also also you need to do this on a local script

Its saying that you cannot index nil with character meaning that Player hasn’t been found, if you are using a serverscript which I hope you are not because UserInputService should only work on the client side then you can just create a function to catch the plr, just like:

Players.PlayerAdded:Connect(function(plr) local character = plr.Character or plr.CharacterAdded:Wait()

Now, it shouldn’t show an error as you have gotten the player parameter.

Also, theres no need in added a loop to check whether the CameraType is scriptable, if you aren’t confident in believing that it won’t change to scriptable then you can change the local script’s name to CameraScript or Camera so that you can take over the Camera default scripts.

Well I wasn’t using a local script but now I am and still it doesn’t work idk why, and here is my code for now:

local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local humanoidroot = Char:WaitForChild("HumanoidRootPart")
--local Cam = workspace.Camera
local cam = workspace.CurrentCamera
local UIS = game:GetService("UserInputService")
local hitbox = game.Workspace.hitboxes.furniture_store
local distance = hitbox.distance.Value

UIS.InputBegan:Connect(function(keyCode)
	if keyCode == Enum.KeyCode.E then
			if (hitbox.Position - humanoidroot.Position).magnitude < distance then
					repeat wait()
        					cam.CameraType = Enum.CameraType.Scriptable
        				wait()
    						until cam.CameraType ==  Enum.CameraType.Scriptable

    					cam.CFrame = workspace.FurnitureStoreCam.CFrame
			end
		end
end)

Can you show me what error is occuring? It would help.

You’re meant to add:

and it should work.

3 Likes