I’m working on a viewmodel handler script, and I’m currently working on shooting.
The model inside of the “backpack†folder is randomly removing from the game. I’ve tried as many bugfixes as i can think of, but nothing is working. It seems to be happening on this line:
local backpackFolder = script:WaitForChild("backpack")
repeat wait() until backpackFolder:FindFirstChildWhichIsA("Model")
print(backpackFolder:FindFirstChildWhichIsA("Model").Name)
backpackFolder.Parent = replicatedStorage
print(backpackFolder:FindFirstChildWhichIsA("Model").Name)
wait(3)
print(backpackFolder:FindFirstChildWhichIsA("Model").Name)
I tried printing the model immediatly after it was added to the backpackfolder, and it came back as “AUG†(The name of the model) but after three seconds… it’s nil? I am not destroying anything in this script, other than the viewmodel when the player dies, but the model isn’t even in the viewmodel?
I first though it was a simple problem with my code, such as the child not existing yet when I try defining it, but it obviously wasn’t that because my bugfixes didn’t work. I looked all over the Developer Hub, and forum, and nothing.
This is my code. I don’t see anything really wrong with it, so I don’t know how to fix this.
local runService = game:GetService("RunService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local userInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character.Humanoid
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()
local viewmodelEnabled = script:WaitForChild("viewmodelEnabled")
local viewmodel = script:WaitForChild("viewmodel")
viewmodel.Parent = workspace
humanoid.Died:Connect(function()
viewmodel:Destroy()
end)
local backpackFolder = script:WaitForChild("backpack")
repeat wait() until backpackFolder:FindFirstChildWhichIsA("Model")
print(backpackFolder:FindFirstChildWhichIsA("Model").Name)
backpackFolder.Parent = replicatedStorage
print(backpackFolder:FindFirstChildWhichIsA("Model").Name)
wait(3)
print(backpackFolder:FindFirstChildWhichIsA("Model").Name)
runService.RenderStepped:Connect(function(deltaTime)
if viewmodelEnabled.Value and viewmodel then
viewmodel:WaitForChild("basePart"):PivotTo(camera.CFrame)
end
end)
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if not gameProcessedEvent and viewmodelEnabled.Value and viewmodel then
if input.KeyCode == Enum.KeyCode.One then
if not viewmodel:FindFirstChildWhichIsA("Model") then
local weapon = backpackFolder:FindFirstChildWhichIsA("Model")
weapon.Parent = viewmodel
viewmodel.basePart.grip.Part1 = weapon.grip
else
local weapon = viewmodel:FindFirstChildWhichIsA("Model")
weapon.Parent = backpackFolder
viewmodel.basePart.grip.Part1 = nil
end
end
end
end)
-- shooting
repeat wait() until backpackFolder:FindFirstChildWhichIsA("Model")
local gunSettings = require(backpackFolder:FindFirstChildWhichIsA("Model").gunSettings)
local ammo = gunSettings.maxAmmo
-- automatic
local mouseDown = false
local canShoot = true
mouse.Button1Down:Connect(function()
mouseDown = true
while mouseDown
and viewmodel:FindFirstChildWhichIsA("Model")
and gunSettings.fireType == "auto"
and canShoot
and ammo > 0
do
ammo = ammo - 1
print("gyat")
canShoot = false
task.wait(1 / gunSettings.fireRate)
canShoot = true
end
end)
mouse.Button1Up:Connect(function()
mouseDown = false
end)
My explorer looks like this:
1 post - 1 participant