Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

part is in explorer but localscript cant see it, any help?

Asked by 1 year ago

I am trying to make an FPS gun system, it works just fine until the player dies. I can still see the "ViewModel" object in the explorer, but when I try to reference it through a modulescript that is run by a localscript it can't see it. Any help would be appreciated!

local script:

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local modules = game:GetService("ReplicatedStorage").Modules
local toolData = require(modules.ToolData)
local keybinds = require(modules.Keybinds)
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input)
    if keybinds.inputMethodReturn == "kbm" then
        for _, actionClass in keybinds do
            if type(actionClass) == "string" then
                continue
            end
            for action, keycode in actionClass do
                if input.KeyCode == keycode or input.UserInputType == keycode then
                    print(action, keycode)
                    toolData.parseInputs(action, character)
                end
            end
        end
    end
end)
toolData.playerSetup(player)
local function onSpawned(character)
    toolData.playerSetup(player)
end
local function onDied()
    toolData.playerDied(player)
end
character.Humanoid.Died:Connect(onDied)
player.CharacterAdded:Connect(function(character)
    onSpawned(character)
end)

module script:

local toolData = {}
toolData.rightArmGrip = "Armdir"
toolData.leftArmGrip = "Armdir"
toolData.armSize = Vector3.new(1,1,5)
toolData.shoulderOffset = Vector3.new(2, -1.5, 0)
toolData.gunRotation = CFrame.Angles(math.rad(0),math.rad(0),math.rad(0))
toolData.maxRotationPerSec = 50
fpsconnection = nil
local function updateViewModel(gunWeld, left, leftWeld, right, rightWeld, character)
    local x,y,z = workspace.CurrentCamera.CFrame:ToOrientation()
    local gun = toolData.getHeldTool(character)
    gunWeld.C0 = CFrame.new(toolData.shoulderOffset)*CFrame.Angles(x,0,0)
    gunWeld.C1 = gun.Stats.Offset.Value * toolData.gunRotation
    leftWeld.C0 = gun.Grip[toolData.leftArmGrip].CFrame
    leftWeld.C1 = CFrame.new(0,0,left.Size.Z / 2)
    rightWeld.C0 = gun.Handle[toolData.rightArmGrip].CFrame
    rightWeld.C1 = CFrame.new(0,0,toolData.armSize.Z/2)

    for _, part in toolData.getHeldTool(character).Parent:GetDescendants() do
        if part:IsA("BasePart") then
            part:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
                part.LocalTransparencyModifier = part.Transparency
            end)
            part.LocalTransparencyModifier = part.Transparency
        end
    end
end
local function spawnGun(gunType, character, ViewModel)
    print("gun spawned")
    local x,y,z = workspace.Camera.CFrame:ToOrientation()
    local lookAngle = Vector3.new(x,y,z)
    local left = Instance.new("Part")
    left.Size = toolData.armSize
    left.BrickColor = BrickColor.new(Color3.fromRGB(227, 168, 101))
    left.Massless = true
    left.CanCollide = false
    left.Parent = character:FindFirstChildOfClass("Model")
    local right = Instance.new("Part")
    right.Size = toolData.armSize
    right.BrickColor = BrickColor.new(Color3.fromRGB(227, 168, 101))
    right.Massless = true
    right.CanCollide = false
    right.Parent = character:FindFirstChildOfClass("Model")
    local gun = game:GetService("Players")[character.Name].GunsFolder[gunType]:GetChildren()[1]:Clone()
    gun.Parent = character:FindFirstChildOfClass("Model")
    local gunWeld = Instance.new("Weld")
    gunWeld.Part0 = toolData.getHeldTool(character).Parent.HeadAnchor
    gunWeld.C0 = CFrame.new(toolData.shoulderOffset)*CFrame.Angles(0,y,0)
    gunWeld.Part1 = gun.Handle
    --add grippos as an offset to take into account below
    gunWeld.C1 = gun.Stats.Offset.Value * toolData.gunRotation
    gunWeld.Parent = character:FindFirstChildOfClass("Model").Welds
    local leftWeld = Instance.new("Weld")
    leftWeld.Part0 = gun.Grip
    leftWeld.C0 = gun.Grip[toolData.leftArmGrip].CFrame
    leftWeld.Part1 = left
    leftWeld.C1 = CFrame.new(0,0,toolData.armSize.Z/2)
    leftWeld.Parent = character:FindFirstChildOfClass("Model").Welds
    local rightWeld = Instance.new("Weld")
    rightWeld.Part0 = gun.Handle
    rightWeld.C0 = gun.Handle[toolData.rightArmGrip].CFrame
    rightWeld.Part1 = right
    rightWeld.C1 = CFrame.new(0,0,toolData.armSize.Z/2)
    rightWeld.Parent = character:FindFirstChildOfClass("Model").Welds
    fpsconnection = game:GetService("RunService").RenderStepped:Connect(function()
        updateViewModel(gunWeld, left, leftWeld, right, rightWeld, character)
    end)
end
function toolData.playerDied(player)
    fpsconnection:Disconnect()
    local viewModel = player.Character.ViewModel
    viewModel.Parent = player
    player.CharacterAdded:Wait()
    viewModel.Parent = player.Character
    print(viewModel)
    print(viewModel.Parent)
end
function toolData.getHeldTool(character)
    return character:FindFirstChildOfClass("Model"):FindFirstChildOfClass("Model") or nil
end
function toolData.playerSetup(player)
    print("spawned")
    player.CameraMode = Enum.CameraMode.LockFirstPerson
    if not player.Character:FindFirstChild("ViewModel") then
        print("making viewmodel")
        local viewModel = Instance.new("Model")
        viewModel.Name = "ViewModel"
        viewModel.Parent = player.Character or player.CharacterAdded:Wait()
    end
    local headAnchor = Instance.new("Part")
    headAnchor.Parent = player.Character.ViewModel
    headAnchor.Transparency = 1
    headAnchor.CanCollide = false
    headAnchor.Massless = true
    headAnchor.Name = "HeadAnchor"
    local welds = Instance.new("Folder")
    welds.Name = "Welds"
    welds.Parent = player.Character.ViewModel
    local headAnchorWeld = Instance.new("Weld")
    headAnchorWeld.Part0 = player.Character:WaitForChild("HumanoidRootPart")
    headAnchorWeld.Part1 = headAnchor
    headAnchorWeld.C0 = CFrame.new(0, 1.5, 0)
    headAnchorWeld.Parent = welds
    if player:FindFirstChild("gunsFolder") then return end
    print("gunsfolder setup")
    local gunsFolder = Instance.new("Folder")
    gunsFolder.Name = "GunsFolder"
    gunsFolder.Parent = player
    local primaryFolder = Instance.new("Folder")
    primaryFolder.Name = "Primary"
    primaryFolder.Parent = gunsFolder
    local secondaryFolder = Instance.new("Folder")
    secondaryFolder.Name = "Secondary"
    secondaryFolder.Parent = gunsFolder
    local gun = game:GetService("ReplicatedStorage").Weapons.Primary.gun:Clone()
    gun.Parent = primaryFolder
end
function toolData.parseInputs(action, character)
    if action == "Shoot" then
        --shoot gun
    elseif action == "aim" then
        --aim gun
    elseif action == "reload" then
        toolData.rightArmGrip = "Boltpos"
        wait(1)
        toolData.rightArmGrip = "Armdir"
        --reload
    elseif action == "leanLeft" then
        --lean left
    elseif action == "leanRight" then
        --lean right
    elseif action == "stanceDown" then
        -- stance down
    elseif action == "stanceUp" then
        --stance up
    elseif action == "primary" then
        if fpsconnection then fpsconnection:Disconnect() end
        spawnGun("Primary", character)
    elseif action == "secondary" then
        if fpsconnection then fpsconnection:Disconnect() end
        --switch to secondary
    elseif action == "tertiary" then
        --switch to tertiary
    elseif action == "switch" then
        --switch guns
    end
end
return toolData

the 3rd script just stores the keybinds for whatever input method the player is using, its not important because thats not where the error happens the error is on line 37 in the module script saying that "ViewModel" is an invalid member of my username (character)

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

In your module script I don't see you told the system where it is you are going to want to do something like this

local View = game.Workspace.ViewModel -- change this to the correct placement it is

or depending on the scripts location you can do this

local View = script.Parent.ViewModel --add Parents until ViewModel comes up
0
i believe i did that in the toolData.setupPlayer function, but it might not be saved across the entire script. Ill try what you said after im done with school as I dont have enough time rn before school. Verse_NOVA 52 — 1y
0
ok 666_brithday 103 — 1y
0
it works, im getting a different error message! thanks! Verse_NOVA 52 — 1y
0
I completely fixed it now, no more problems :D Verse_NOVA 52 — 1y
View all comments (2 more)
0
I completely fixed it now, no more problems :D Verse_NOVA 52 — 1y
0
No problem 666_brithday 103 — 1y
Ad

Answer this question