I've made a very simple gun script that when Mouse.Target.Parent has a humanoid inside of it, it fires a remoteevent that causes that humanoid to be damaged. If I were to equip the tool, the gun works completely fine. But when I unequip and reequip the gun whenever I fire I get an error that states "This Mouse is no longer active." If someone could help me that would be greatly appreciated! Thanks!
Local script inside of the tool:
local MaxAmmo = 5
local Ammo = MaxAmmo
local Reloading = false
local Player = game.Players.LocalPlayer
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Icon = "rbxasset://textures/GunCursor.png"
local function Reload()
Reloading = true
Mouse.Icon = "rbxasset://textures/GunWaitCursor.png"
wait(2)
Ammo = MaxAmmo
Reloading = false
Mouse.Icon = "rbxasset://textures/GunCursor.png"
end
script.Parent.Activated:Connect(function()
if Ammo > 0 and not Reloading then
Ammo = Ammo -1
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
game.ReplicatedStorage.TakeDamage:FireServer(Mouse.Target.Parent.Humanoid)
elseif Mouse.Target == nil then
print("Didn't shoot at a brick")
end
elseif Reloading == false then
Reload()
end
print(Ammo)
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)
Server script:
game.ReplicatedStorage.TakeDamage.OnServerEvent:Connect(function(player, SpecHumanoid)
SpecHumanoid:TakeDamage(20)
end)
