E to turn on light (oop)

Hello, i want to make a light turn on when you press “e” if you are 15 studs away, how do i do this?

i have already made the light turn on if you press e but it will work when i am very far away, how do i fix this?

1 Like

You can get the Magnitude (distance) between your HumanoidRootPart and the light.

local distance = (Player.Character.HumaoidRootPart.Position - Workspace.Light.Position).Magnitude
if  distance <= 15 then
--toggle the light 
end
1 Like

how would i make this in an InputEnded function though?

1 Like

so that i can make it so it will toggle when i type “E”

1 Like

That should be pretty simple.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
Mouse.KeyDown:Connect(function(Key)
local distance = (Player.Character.HumaoidRootPart.Position - Workspace.Light.Position).Magnitude
if  Key == "e" and distance <= 15 then
--toggle the light 
end
end)

This goes in a LocalScript, so you should use a Remote to turn on the light.

1 Like

I have just tested this in studio and it does not work.

Gotta learn the basics dude.

Do not use the getmouse function in @HappleMan’s reply as that’s not efficient anymore, and is better to use UIS.

4 Likes

I know UserInputService but i don’t know how to put it in the script.

local UIS = game:GetService('UserInputService')

UIS.InputBegan:Connect(function(InputType, GPE)
	if not GPE then
		if InputType.KeyCode == Enum.KeyCode.E then
			print('E was pressed')
		end
	end
end)
2 Likes

i know this but i cant use the distance script in it, it wont work

Hey, it’s good that you are asking for help using dev forum but the point isn’t to code everything for you. We aren’t supposed to give you scripts but pushing you at the right direction

4 Likes