F for Flashlight non-tool

Heyo,
I’ve been playing around with pressing F for a Flashlight in my game, a lot of tutorials rely on having a flashlight appear in your inventory as a tool which isn’t the clean look I’m going for.

I would like to try and make a Flashlight appear on the Player’s hand so they are holding it through some other means that isn’t related to tools or at least the backpack.

I’ve looked around and Motor6D seems to be the way to do it but I’m a bit confused and in need of either help or an alternative method.

cheers

1 Like

I believe you can use UserInputService (UIS) to detect player inputs (such as pressing keys) and then create a Light instance have it parented to the tool.

For Motor6D’s you can basically do:

local Motor6D = Instance.new("Motor6D")
Motor6D.Parent = -- whereever you want
Motor6D.Part0 = -- Where you want to weld the flashlight
Motor6D.Part1 = -- the main part of the flashlight

Do note that if your item you want to hold to the player has multiple parts you need to have all the parts welded together too by using Motor6D or simply welds.

Yeah I’ve got the input part working fine,
due to me using r6 (I’m assuming this because it happened earlier when I ran similar code) does that mean in order to have the flashlight actually held in the player’s hand and not simply placed into their arm I would have to use an animation?

You can leave the flashlight originally placed IN their arm or wherever.
Then you can have the animations put them into place

I played around with this for about an hour and for some reason the Motor6D just refuses to do anything it is like it doesn’t even exist.

Once the attachments are established the Part/Flashlight should just stick to the Arm as if it was a weld right?

May I ask, do you have a Light Source in the player’s character already, or do you want the F to Control script to create a light within the player?

Using a LocalScript, you can make a basic flashlight system. Keep in mind, your flashlight as shown here will not orientate to the direction your camera is facing directly, and will only face directly in-front of the HumanoidRootPart, which works in some games by just lighting up the area in front of the player.

(This script should be in StarterGui, or any Local Player folder like StarterCharacterScripts)

Player = game.Players.LocalPlayer
Character = Player.Character
UIS = game:GetService("UserInputService")
LightSource = Instance.new("SpotLight",Character:WaitForChild("HumanoidRootPart"))
LightSource.Enabled = false
-- Feel free to modify the light source to your own preferences.
local function onKeyPress(input)
    if input.KeyCode == Enum.KeyCode.F then
        LightSource.Enabled = not LightSource.Enabled
    end
end

UIS.InputBegan:Connect(onKeyPress)

Try this code. Just make sure it’s in a LocalScript inside of StarterCharacterScripts, and you have your flashlight in ReplicatedStorage named “Flashlight”. It’s only works well when the player is standing, so I’ll update it soon:

local uis = game:GetService("UserInputService")
local hasflashlight = false
local flashlight = nil

uis.InputBegan:Connect(function(key)
	if key.UserInputType == Enum.UserInputType.Keyboard then
		if key.KeyCode == Enum.KeyCode.F then
			if hasflashlight == false then
				hasflashlight = true

				local char = script.Parent

				flashlight = game:GetService("ReplicatedStorage").Flashlight:Clone()

				flashlight.Parent = char
				
				local Motor6D = Instance.new("Motor6D")
				Motor6D.Parent = char
				Motor6D.Part0 = char:FindFirstChild("Right Arm")
				Motor6D.Part1 = flashlight
				
				flashlight.Position = char:FindFirstChild("Right Arm").Position + Vector3.new(0, -1, 0)
				flashlight.Orientation = char:FindFirstChild("Right Arm").Orientation
			else
				flashlight:Destroy()
				flashlight = nil
				hasflashlight = false
			end	
		end
	end
end)
1 Like

Mind showing me a screenshot?
The flashlight should stick to the arm if you did it correctly. If your flashlight has multiple parts consider welding each other together using Motor6D.

Thank you this actually works pretty well and I can most likely achieve what I’d like with this, one thing though is how would I go about making this server sided?
I would like it so that all players can see whether a player is holding a flashlight or not.

@MikeartsRBLX
I ended up using the script @J_Angry supplied as a base for my script which works well, I guess now I just need to make it server sided,.

I appreciate the work here but I’m wanting more of a tool being held than a light inside a player

If you want the flashlight to be activated on the server side, just make a new script and put it in ServerScriptService, and when the F key is pressed, call a RemoteEvent. Here’s an example if it helps. (The code was also updated, so that the flashlight now will ALWAYS attach to the player’s hand.)

-- LocalScript
local uis = game:GetService("UserInputService")
local remoteevent = game:GetService("ReplicatedStorage").RemoteEvent

uis.InputBegan:Connect(function(key)
	if key.UserInputType == Enum.UserInputType.Keyboard then
		if key.KeyCode == Enum.KeyCode.F then
			remoteevent:FireServer()
		end
	end
end)
-- Server Script (in ServerScriptService)
local remoteevent = game:GetService("ReplicatedStorage").RemoteEvent

local hasflashlight = false
local flashlight = nil
local plr = game:GetService("Players").PlayerAdded:Wait()

remoteevent.OnServerEvent:Connect(function()
	if hasflashlight == false then
		hasflashlight = true
	
		local char = plr.Character or plr.CharacterAdded:Wait()
	
		flashlight = game:GetService("ReplicatedStorage").Flashlight:Clone()
	
		flashlight.Parent = char
					
		local Motor6D = Instance.new("Motor6D")
		Motor6D.Parent = char
		Motor6D.Part0 = char:FindFirstChild("Right Arm")
		Motor6D.Part1 = flashlight
		Motor6D.C1 = CFrame.new(Vector3.new(0, 1, 0))
	else
		flashlight:Destroy()
		flashlight = nil
		hasflashlight = false
	end	
end)
3 Likes

Apologies for the late reply as I’ve been busy,
Thank you so much for your efforts and support everything you’ve shown me to do has worked perfectly as I wished it to.

Now I just need to work on some orientation stuff and light things which’ll be fun,
Cheers!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.