I have this gun script that works fine for the default gun it is in, but whenever I try to put a different gun model (with all parts included to make it work), it says 'Muzzle is not a valid member of Tool' in the output. Please help.
local userInputService = game:GetService("UserInputService")
local tool = script.Parent.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character
local characterState = character:WaitForChild("State")
local state = tool:WaitForChild("State")
local config = require(tool:WaitForChild("Config"))
local model = character:WaitForChild(tool.Name .. "Model")
local modelParts = {}
for _, v in pairs(model:GetChildren()) do
table.insert(modelParts, {v, v.Transparency})
end
local canShoot = true
local clicking = false
local equipped = false
state.Changed:connect(function()
tool.Remotes.ChangeState:FireServer(state.Value)
if state.Value == "Aim" then
if characterState.Value == "Sprint" then
characterState.Value = "None"
end
end
end)
characterState.Changed:connect(function()
if characterState.Value == "Sprint" then
if state.Value ~= "Reload" then
state.Value = "Sprint"
end
elseif state.Value == "Sprint" then
state.Value = "None"
end
end)
local function Shoot()
tool.Muzzle.FireSound:Play()
tool.Muzzle.FlashEmitter:Emit(1)
tool.ShellEject.ShellEmitter:Emit(1)
tool.Ammo.Value = tool.Ammo.Value - 1
local bullets = {}
for i = 1, config.Stats.Bullets do
local spread = config.Stats.Spread * 5
local position = tool.Muzzle.Position
local direction = (CFrame.new(position, mouse.Hit.p) * CFrame.Angles(math.rad(math.random(-spread, spread) / 10), math.rad(math.random(-spread, spread) / 10), 0)).lookVector
table.insert(bullets, {position, direction, config.Stats.Velocity + math.random(-20, 20)})
end
game.ReplicatedStorage.Events.FireBullets:Fire(bullets)
tool.Remotes.Fire:FireServer(bullets)
end
local function Reload()
if state.Value ~= "Reload" then
state.Value = "Reload"
tool.Remotes.Reload:FireServer()
if tool:FindFirstChild("Magazine") then
tool.Magazine.ReloadSound:Play()
local mag = tool.Magazine:Clone()
mag.Parent = character
mag.CanCollide = false
game.Debris:AddItem(mag, 10)
tool.Magazine.LocalTransparencyModifier = 1
end
tool.Ammo.Value = 0
wait(config.Stats.ReloadTime)
if tool:FindFirstChild("Magazine") then
tool.Magazine.LocalTransparencyModifier = tool.Magazine.Transparency
end
if equipped then
if tool:FindFirstChild("Magazine") then
tool.Magazine.ReloadFinishedSound:Play()
end
if characterState.Value == "Sprint" then
state.Value = "Sprint"
else
state.Value = "None"
end
else
state.Value = "None"
end
end
end
userInputService.InputBegan:connect(function(inputObject, processed)
if equipped and not processed then
if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
clicking = true
if config.Stats.Automatic then
while canShoot and clicking and equipped and tool.Ammo.Value > 0 and state.Value ~= "Sprint" and state.Value ~= "Reload" do
canShoot = false
Shoot()
wait(1 / config.Stats.RateOfFire)
canShoot = true
end
else
if canShoot and tool.Ammo.Value > 0 and state.Value ~= "Sprint" and state.Value ~= "Reload" then
canShoot = false
Shoot()
wait(1 / config.Stats.RateOfFire)
canShoot = true
end
end
elseif inputObject.UserInputType == Enum.UserInputType.MouseButton2 then
if state.Value ~= "Reload" then
state.Value = "Aim"
end
elseif inputObject.KeyCode == Enum.KeyCode.Q then
if state.Value == "Aim" then
state.Value = "None"
elseif state.Value ~= "Reload" then
state.Value = "Aim"
end
elseif inputObject.KeyCode == Enum.KeyCode.R then
Reload()
end
end
end)
userInputService.InputEnded:connect(function(inputObject, processed)
if equipped and not processed then
if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
clicking = false
elseif inputObject.UserInputType == Enum.UserInputType.MouseButton2 then
if state.Value == "Aim" then
state.Value = "None"
end
end
end
end)
tool.Equipped:connect(function()
equipped = true
mouse.Icon = config.Cursor
local motor = Instance.new("Motor6D", character.Torso)
motor.Name = "TempToolGrip"
motor.Part0 = character.Torso
motor.Part1 = tool.MainGrip
motor.C0 = config.Attachment
if characterState.Value == "Sprint" and state.Value ~= "Reload" then
state.Value = "Sprint"
end
for _, v in pairs(modelParts) do
v[1].Transparency = 1
end
end)
tool.Unequipped:connect(function()
equipped = false
mouse.Icon = ""
if state.Value ~= "Reload" then
state.Value = "None"
end
for _, v in pairs(modelParts) do
v[1].Transparency = v[2]
end
end)

1 comment
Just by the way, having local variables is only useful when the variables are in a loop or some type of local function.