#Viewmodel script not really working as planned for my fps

1 messages · Page 1 of 1 (latest)

celest rivet
#

so i have a script that is suppost to detect whenever your holding a item, when your holding that item it kinda summons the Viewmodel, but after about 5 seconds the item gets removed from my inventory and the Viewmodel stays floting in the air/in its last position. does anyone know a fix for this?

#

ill try to post the script i used

#
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

local viewmodelPath = game.ReplicatedStorage:WaitForChild("Viewmodel") 
local viewmodelModel = viewmodelPath:Clone()
viewmodelModel.Parent = camera

local requiredToolName = "TROY DEFENSE AR"

local viewmodelOffset = CFrame.new(0, 0, 0) 

local function isHoldingTool(toolName: string): boolean
    if player.Character then
        local tool = player.Character:FindFirstChild(toolName)
        return tool ~= nil and tool:IsA("Tool")
    end
    return false
end

local function updateCamera()
    if isHoldingTool(requiredToolName) and viewmodelModel then
        viewmodelModel:PivotTo(camera.CFrame * viewmodelOffset)
    end
end

RunService.RenderStepped:Connect(updateCamera)

if player.Character then
    player.Character.ChildAdded:Connect(function(child)
        if child:IsA("Tool") and child.Name == requiredToolName then
            for _, part in ipairs(player.Character:GetDescendants()) do
                if part:IsA("BasePart") or part:IsA("Decal") then
                    part.Transparency = 1
                end
            end
        end
    end)

    player.Character.ChildRemoved:Connect(function(child)
        if child:IsA("Tool") and child.Name == requiredToolName then
            for _, part in ipairs(player.Character:GetDescendants()) do
                if part:IsA("BasePart") or part:IsA("Decal") then
                    part.Transparency = 0
                end
            end
        end
    end)
end
stark pivot
#

create the viewmodel when the tool is equipped

#

destroy it when the tool is unequipped

thin minnow
#
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

local viewmodelPath = game.ReplicatedStorage:WaitForChild("Viewmodel") 
local viewmodelModel = nil  -- start empty

local requiredToolName = "TROY DEFENSE AR"
local viewmodelOffset = CFrame.new(0, 0, 0)

local function isHoldingTool(toolName: string): boolean
    if player.Character then
        local tool = player.Character:FindFirstChild(toolName)
        return tool ~= nil and tool:IsA("Tool")
    end
    return false
end

local function updateCamera()
    if isHoldingTool(requiredToolName) then
        if not viewmodelModel then
            viewmodelModel = viewmodelPath:Clone()
            viewmodelModel.Parent = camera
        end
        viewmodelModel:PivotTo(camera.CFrame * viewmodelOffset)
    else
        if viewmodelModel then
            viewmodelModel:Destroy()
            viewmodelModel = nil
        end
    end
end

RunService.RenderStepped:Connect(updateCamera)

if player.Character then
    player.Character.ChildAdded:Connect(function(child)
        if child:IsA("Tool") and child.Name == requiredToolName then
            for _, part in ipairs(player.Character:GetDescendants()) do
                if part:IsA("BasePart") or part:IsA("Decal") then
                    part.Transparency = 1
                end
            end
        end
    end)

    player.Character.ChildRemoved:Connect(function(child)
        if child:IsA("Tool") and child.Name == requiredToolName then
            for _, part in ipairs(player.Character:GetDescendants()) do
                if part:IsA("BasePart") or part:IsA("Decal") then
                    part.Transparency = 0
                end
            end
        end
    end)
end
#

This should fix it

celest rivet
#

that did make the Viewmodel goes away but how do i make the item stay in my inventory, i need it to stay and not just get deleted

thin minnow
#

@celest rivet

#

there are some ways we can do it..

celest rivet
#

And how do I do it

thin minnow
#
  1. put the TROY DEFENSE AR into the starterpack
#

is that what you want

celest rivet
#

Alright

thin minnow
#

or is the tool in ReplicatedStorage?

celest rivet
#

The tool is in the starterpack, that way I can have it at start of game

simple pagodaBOT
#

studio** You are now Level 1! **studio

celest rivet
#

Huh?

thin minnow
#

this: that did make the Viewmodel goes away but how do i make the item stay in my inventory, i need it to stay and not just get deleted

#

need any help?

celest rivet
#

Yeah I still need help

#

It's still being deleted from my inventory and idk why

thin minnow
#

...