This is my gun script. The problem occurs when it comes to reloading. I get this error:
Players.Towren.Backpack.HK416.MainScript:25: attempt to index function with ‘Play’
Here is my code:
wait(0.001)
local maxAmmo = 30
local Ammo = maxAmmo
local reloading = false
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local textLabel = playerGui:WaitForChild("HK416Ammo"):FindFirstChild("AmmoText")
local humanoid = game.Players.LocalPlayer.Character:FindFirstChildOfClass("Humanoid")
local animator = humanoid:FindFirstChildOfClass("Animator")
local reload = animator:LoadAnimation(script.Parent.Anims:FindFirstChild("Reload"))
local holdAnim = animator:LoadAnimation(script.Parent.Anims:FindFirstChild("Hold"))
local Mouse = game.Players.LocalPlayer:GetMouse()
script.Parent.Equipped:Connect(function()
holdAnim:Play()
textLabel.Parent.Enabled = true
local function reload()
local reloadSound = Instance.new("Sound")
reloadSound.SoundId = "rbxassetid://144798533"
reloadSound.Parent = script.Parent
reloadSound.Playing = true
reloadSound.PlaybackSpeed = 1.5
reloading = true
reload:Play()
wait(2.5)
Ammo = maxAmmo
reloading = false
end
script.Parent.Activated:Connect(function()
if Ammo > 0 and not reloading then
Ammo = Ammo - 1
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://168143115"
sound.Parent = script.Parent
sound.Playing = true
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 15)
end
elseif reloading == false then
reload()
end
while wait() do
textLabel.Text = (Ammo).." / "..maxAmmo
end
end)
local input = game:GetService("UserInputService")
input.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.R and reloading == false and Ammo ~= maxAmmo then
reload()
end
end)
end)
script.Parent.Unequipped:Connect(function()
holdAnim:Stop()
textLabel.Parent.Enabled = false
end)
Anyone got any solutions?