Making tool invisible locally when player entered first person

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

- Making tool Invisible (locally) when player entered first person

  1. What is the issue? Include screenshots / videos if possible!

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    - So far none seems solving my problem

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

- I used module script (Not OOP?) for my viewmodel and other stuffs

- Used Head LocalTransparencyModifier to track if player is in first person

- I could add extra details now but i think i cant reply because im busy IRL, so if you see me not replying quick please be paitent please.

7 Likes

Can you provide your scripts?
It’s hard to really help without it.

2 Likes

here.
Tool LocalScript:

local tool = script.Parent
local RepStore = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local FireCooldown = false
local ReloadCooldown = false

local bullets = 10

tool.Equipped:Connect(function()
	for i, v in pairs(char:WaitForChild("Right Arm"):GetChildren()) do
		if v:IsA("Weld") and v.Part1 == tool.C96.Handle then
			v:Destroy()
		end
	end
	
	local m6d = Instance.new("Motor6D", char:WaitForChild("Torso"))
	m6d.Name = "ToolWeld"
	m6d.Part0 = char:WaitForChild("Torso")
	m6d.Part1 = tool.C96.Handle
	
	local VM = workspace.CurrentCamera:FindFirstChildOfClass("Model")
	local ModelClone = tool.C96:Clone()
	ModelClone.Parent = VM
	
	for i, v in pairs(ModelClone:GetChildren()) do
		if v:IsA("BasePart") or v:IsA("MeshPart") then
			v.CanCollide = false
		end
	end
	
	local m6d2 = VM:WaitForChild("Torso").ToolWeld
	m6d2.Part1 = ModelClone.PrimaryPart
	
	if bullets > 0 then
		local IdleAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("3rdPerson").Idle)
		IdleAnim:Play()
		
		local IdleAnim1 = VM:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("1stPerson").VM_Idle)
		IdleAnim1:Play()
	elseif bullets == 0 then
		local IdleAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("3rdPerson").EmptyGun_Idle)
		IdleAnim:Play()
		
		local IdleAnim1 = VM:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("1stPerson").VM_EmptyIdle)
		IdleAnim1:Play()
	end
end)


tool.Activated:Connect(function()
	if bullets > 0 and not FireCooldown and not ReloadCooldown then
		FireCooldown = true
		local FireAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("3rdPerson").Fire)
		FireAnim:Play()
		
		local VM = workspace.CurrentCamera:FindFirstChildOfClass("Model")
		
		local FireAnim1 = VM:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("1stPerson").VM_Fire)
		FireAnim1:Play()

		local RoundsFired = 1
		bullets = (bullets - RoundsFired)
		print(bullets)
		
		task.wait(.25)
		FireCooldown = false
		
	elseif bullets <= 0 then
		ReloadCooldown = true
		local ReloadAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("3rdPerson").Reload)
		ReloadAnim:Play()

		local VM = workspace.CurrentCamera:FindFirstChildOfClass("Model")

		local ReloadAnim = VM:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("1stPerson").VM_Reload)
		ReloadAnim:Play()
		
		local newBullets = 10
		bullets = newBullets
		print(bullets)
		
		task.wait(2)
		ReloadCooldown = false
	elseif bullets == 1 then
		FireCooldown = true
		local FireAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("3rdPerson").Fire)
		FireAnim:Play()
		
		local IdleAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("3rdPerson").EmptyGun_Idle)
		IdleAnim:Play()

		local VM = workspace.CurrentCamera:FindFirstChildOfClass("Model")

		local FireAnim1 = VM:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("1stPerson").VM_Fire)
		FireAnim1:Play()
		
		local IdleAnim1 = VM:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("1stPerson").VM_EmptyIdle)
		IdleAnim1:Play()

		local RoundsFired = 1
		bullets = (bullets - RoundsFired)
		print(bullets)

		task.wait(.25)
		FireCooldown = false
	end
end)

UIS.InputBegan:Connect(function(input, gp)
	if gp then return end
	
	if input.KeyCode == Enum.KeyCode.R and not ReloadCooldown then
		ReloadCooldown = true
		local ReloadAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("3rdPerson").Reload)
		ReloadAnim:Play()
		
		local VM = workspace.CurrentCamera:FindFirstChildOfClass("Model")
		
		local ReloadAnim = VM:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("1stPerson").VM_Reload)
		ReloadAnim:Play()
		
		task.wait(2)
		ReloadCooldown = false
		local newBullets = 10
		bullets = newBullets
		print(bullets)
	end
end)

tool.Unequipped:Connect(function()
	for i, v in pairs(char:WaitForChild("Torso"):GetChildren()) do
		if v:IsA("Motor6D") and v.Part1 == tool.C96.Handle then
			v:Destroy()
		end
	end
	
	for i, v in pairs(char:WaitForChild("Humanoid"):GetPlayingAnimationTracks()) do
		v:Stop()
	end
	
	local VM = workspace.CurrentCamera:FindFirstChildOfClass("Model")
	
	for i, v in pairs(VM:GetChildren()) do
		if v:IsA("Model") then
			v:Destroy()
		end
	end
	
	for i, v in pairs(VM:WaitForChild("Torso"):GetChildren()) do
		if v:IsA("Motor6D") and v.Name == "ToolWeld" then
			v.Part1 = nil
		end
	end

	for i, v in pairs(VM:WaitForChild("Humanoid"):GetPlayingAnimationTracks()) do
		v:Stop()
	end
end)

ModuleScript

local module = {}

function module.Update(dt, vm, char)
	vm.Parent = workspace.CurrentCamera
	vm.PrimaryPart.CFrame = workspace.CurrentCamera.CFrame
	
	if char:WaitForChild("Head").LocalTransparencyModifier >= 0.5 then
		for i, v in pairs(vm:GetChildren()) do
			if v:IsA("BasePart") then
				v.LocalTransparencyModifier = 0
			end
		end
		
		for i, v in pairs(vm:GetChildren()) do
			if v:IsA("Model") then
				for i, v1 in pairs(v:GetChildren()) do
					if v1:IsA("BasePart") or v1:IsA("UnionOperation") or v1:IsA("MeshPart") then
						v1.LocalTransparencyModifier = 0
					end
				end
			end
		end
		
		for i, v in pairs(char:GetChildren()) do 
			if v:IsA("Tool") then
				for i, v1 in pairs(v:GetChildren()) do
					if v1:IsA("Model") then
						for i, v2 in pairs(v1:GetChildren()) do
							if v2:IsA("BasePart") or v2:IsA("UnionOperation") or v2:IsA("MeshPart") then
								v2.LocalTransparencyModifier = 0
							end
						end
					end
				end
			end
		end
		
	else
		for i, v in pairs(vm:GetChildren()) do
			if v:IsA("BasePart") then
				v.LocalTransparencyModifier = 1
			end
		end
		
		for i, v in pairs(vm:GetChildren()) do
			if v:IsA("Model") then
				for i, v1 in pairs(v:GetChildren()) do
					if v1:IsA("BasePart") or v1:IsA("UnionOperation") or v1:IsA("MeshPart") then
						v1.LocalTransparencyModifier = 1
					end
				end
			end
		end
		
		for i, v in pairs(char:GetChildren()) do 
			if v:IsA("Tool") then
				for i, v1 in pairs(v:GetChildren()) do
					if v1:IsA("Model") then
						for i, v2 in pairs(v1:GetChildren()) do
							if v2:IsA("BasePart") or v2:IsA("UnionOperation") or v2:IsA("MeshPart") then
								v2.LocalTransparencyModifier = 0
							end
						end
					end
				end
			end
		end
		
	end
end

function module.WeldTool(tool, vm)
	local M6D = vm:WaitForChild("Torso").ToolWeld
	
	M6D.Part1 = tool.PrimaryPart
end

function module.UnWeldTool(vm)
	local M6D = vm:WaitForChild("Torso").ToolWeld

	M6D.Part1 = nil
end

return module

also starterplayer script (rendering the viewmodel)

local runServ = game:GetService("RunService")
local RepStore = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")

local wepsFolder = RepStore:WaitForChild("Weapons")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local vm = RepStore:WaitForChild("Viewmodel"):Clone()
vm.Parent = workspace.CurrentCamera
local shirtClone = char:WaitForChild("Shirt"):Clone()
shirtClone.Parent = vm
local bodyclrClone = char:WaitForChild("Body Colors"):Clone()
bodyclrClone.Parent = vm

local ModuleScript = require(RepStore.Modules.ModuleScript)

runServ.RenderStepped:Connect(function(dt)
	ModuleScript.Update(dt, vm, char)
end)
3 Likes

I was able to recreate this in studio (Had to repost because I used a different video)


local player = game.Players.LocalPlayer


local character = player.Character or player.CharacterAdded:Wait()
local tool = character:WaitForChild("Flash Light")



local function MakeToolInvisible()
	tool.Handle.Transparency = 1
end


local function RestoreToolVisibility()
	tool.Handle.Transparency = 0
end


local function IsInFirstPerson()
	local camera = game.Workspace.CurrentCamera
	local head = character:FindFirstChild("Head")

	if camera and head then
		local cameraPosition = camera.CFrame.p
		local headPosition = head.Position
		local distance = (cameraPosition - headPosition).Magnitude
		return distance < 1
	end

	return false
end

local function UpdateToolVisibility()
	if IsInFirstPerson() then
		MakeToolInvisible()
	else
		RestoreToolVisibility()
	end
end

UpdateToolVisibility()

game:GetService("RunService").RenderStepped:Connect(function()
	UpdateToolVisibility()
end)

(I placed this LocalScript in StarterCharacterScripts)

3 Likes

does your tool’s handle contain parts inside of it? if so, you should get all of the children inside of the handle use the :IsA() method to check if the child is a part, then make it transparent.

2 Likes

heres an example:
image

2 Likes

No My Tool DOES not have parts inside it besides motor6d and welds

1 Like

Does it work with tool ?
wait hold up letme check

1 Like

Yes, just replace “Flash Light” with the tool you want on this line

local tool = character:WaitForChild("Flash Light")

if your gun has more than 1 visible part then you can use the :IsA() method to fetch for “Parts” or reference them manually if you want

1 Like

Alright, can you provide me an example of :IsA() method ?

1 Like

Problem here is this game is multiplayer and seeing your enemy shooting you with a nothing gun seems like a bad idea

1 Like

It does turn invisible only for the local player. Did you put it as a LocalScript?

“unknown_2023.08.15-15.22_clip_1”

I don’t know why it hyperlinked it like that but that link leads to the clip I uploaded to streamable

1 Like

I cant see the video, it was in archive. But yes it was in a local script

Check if the player is in first person with
if (camera.Focus.Position - camera.CFrame.Position).Magnitude <= 1 then
And then set all of the tool’s descendants LocalTransparencyModifier property to 1

1 Like

I found the solution a long time ago lol

but still thanks!

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