Whenever I fire the function Hit() it only fires once (i know this because i put print statements) but within the Hit function it fires a function from a module script only once yet the function in the module script sometimes runs 4 times, sometimes runs 1 time, sometimes runs 3 times. I may have it in a loop but i dont think i do
//Local script (the one that fire the modules scripts function)
local mouse = game.Players.LocalPlayer:GetMouse();
local equipped = false;
local swinging = false;
local swingAnime;
local inProccess = false;
local alreadyHit = false
local mouseDown = false;
local delayTime = 0.1;
local minDistance = 15;
local folder = game.Workspace.Island.Rocks;
local folder2 = game.Workspace.Island.Blocks;
local emitter
local tool = script.Parent
local functions = require(game.ReplicatedStorage.Modules.Functions)
local function hitRock()
pcall(function()
if game.Workspace:FindFirstChild("ParticleEffect") then
emitter = game.Workspace.ParticleEffect
emitter.Parent = game.Players.LocalPlayer.Character
else
emitter = game.Players.LocalPlayer.Character.ParticleEffect
end
--//Check For Rocks
for i, rock in ipairs(folder:GetChildren()) do
local distance = (game.Players.LocalPlayer.Character.PrimaryPart.Position - rock.PrimaryPart.Position).Magnitude;
if distance <= minDistance then
mouse.TargetFilter = game.Players.LocalPlayer.Character;
if mouse.Target then
if mouse.Target.Parent then
if mouse.Target.Parent.Parent then
if mouse.Target.Parent.Parent == folder then
local hitting = mouse.Target.Parent
script.Parent.RockHit:Play()
emitter.CFrame = mouse.Hit;
emitter.A.Hit.Color = ColorSequence.new(mouse.Target.Color);
emitter.A.Hit:Emit();
hitting.HealthBar.Enabled = true
local deal = require(game.ReplicatedStorage.Modules.CustomDamageDealt)
local giant = require(game.ReplicatedStorage.Tables.ItemValues.megaTable)
local damage = giant.Damage[script.Parent.Name]
if functions.detectType(hitting) == "Rock" then
if string.lower(script.Parent.Name):find("pickaxe") then
damage *= 2
else
damage /= 2
end
elseif functions.detectType(hitting) == "Wood" then
if string.lower(script.Parent.Name):find("axe") then
damage *= 2
else
damage /= 2
end
else
damage = damage
end
deal:deal(game.Players.LocalPlayer, hitting, damage)
spawn(function()
pcall(function()
local hitting2 = mouse.Target.Parent
local hitagain = false
hitting.Health.Changed:Connect(function()
hitagain = true
end)
wait(2)
if hitagain == false then
hitting2.HealthBar.Enabled = false
end
end)
end)
spawn(function()
pcall(function()
game:GetService("TweenService"):Create(hitting.PrimaryPart, TweenInfo.new(0.1), {Size = rock.PrimaryPart.Size/1.1}):Play()
wait(0.1)
game:GetService("TweenService"):Create(hitting.PrimaryPart, TweenInfo.new(0.1), {Size = rock.PrimaryPart.Size * 1.1}):Play()
end)
end)
end
end
end
end
end
end
--//Check For Blocks
for i, block in ipairs(folder2:GetChildren()) do
local distance = (game.Players.LocalPlayer.Character.PrimaryPart.Position - block.PrimaryPart.Position).Magnitude;
if distance <= minDistance and alreadyHit == false then
mouse.TargetFilter = game.Players.LocalPlayer.Character;
if mouse.Target then
if mouse.Target.Parent then
if mouse.Target.Parent.Parent then
if mouse.Target.Parent.Parent == folder2 then
local hitting = mouse.Target.Parent
if hitting.Name ~= "ParticleEffect" then
alreadyHit = true
functions.SoundPlayer(hitting.Name)
emitter.CFrame = mouse.Hit;
emitter.A.Hit.Color = ColorSequence.new(mouse.Target.Color);
emitter.A.Hit:Emit();
--//Health Removal
local damage = nil
hitting.Health.Value -= damage
hitting.HealthBar.Enabled = true
spawn(function()
pcall(function()
local hitting2 = mouse.Target.Parent
local hitagain = false
hitting.Health.Changed:Connect(function()
hitagain = true
end)
wait(2)
if hitagain == false then
hitting2.HealthBar.Enabled = false
end
end)
end)
hitting.HealthBar.Frame.Inside:TweenSize(UDim2.new(hitting.Health.Value/hitting.MaxHealth.Value, 0, 1, 0),'In','Linear','0.1', true)
if hitting.Health.Value <= 0 then
spawn(function()
local inventory = require(game.ReplicatedStorage.Tables.ItemValues.Inventory)
inventory:AddItem(game.Players.LocalPlayer, hitting.Name, 1, true);
game.ReplicatedStorage.Remotes.Functions.Pickup:InvokeServer(hitting)
end)
spawn(function()
hitting.HealthBar.Frame:TweenSize(UDim2.new(0, 0, 0.05, 0),'In','Linear','0.1', true)
functions.ScaleModel(hitting, 0.9 , 0.1)
wait(0.1)
functions.ScaleModel(hitting, 0.3 , 0.1)
for i,v in pairs(hitting:GetDescendants()) do
if v:IsA("BasePart")then
game:GetService("TweenService"):Create(v, TweenInfo.new(0.3), {Transparency = 1}):Play()
end
end
wait(0.1)
for i,v in pairs(hitting:GetDescendants()) do
if v:IsA("BasePart") and v ~= hitting.PrimaryPart then
local weld = Instance.new("WeldConstraint")
weld.Part0 = v
weld.Part1 = hitting.PrimaryPart
weld.Parent = v
v.Anchored = false
end
if v:IsA("BasePart") then
v.CanCollide = false
end
end
game:GetService("TweenService"):Create(hitting.PrimaryPart, TweenInfo.new(0.3), {CFrame = game.Players.LocalPlayer.Character.PrimaryPart.CFrame}):Play()
wait(0.2)
if hitting.Name ~= "ParticleEffect" then
hitting:Destroy()
end
end)
end
end
end
end
end
end
end
end
end)
end
script.Parent.Equipped:Connect(function()
equipped = true;
--//Load Animations
swingAnime = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid"):LoadAnimation(game.ReplicatedStorage.Animations.ToolSwing);
end)
script.Parent.Unequipped:Connect(function()
equipped = false;
end)
mouse.Button1Down:Connect(function()
mouseDown = true;
end)
mouse.Button1Up:Connect(function()
mouseDown = false;
if equipped == true and inProccess == false then
inProccess = true;
swingAnime:Stop();
wait(swingAnime.Length);
swinging = false;
inProccess = false;
alreadyHit = false
end
end)
while wait() do
if equipped == true and mouseDown == true then
if swinging == false then
swinging = true;
swingAnime:Play();
swingAnime:AdjustSpeed(2)
wait(swingAnime.Length/2);
hitRock();
wait(delayTime);
swinging = false;
alreadyHit = false
end
end
end
Module script
local custom = {}
function detectSide()
local e, s = pcall(function()
return game.Players.LocalPlayer.Name
end)
if not s then
return false
else
return true
end
end
function custom:deal(player, item, damage)
print("Test")
local health = item.Container.health
local max = item.Container.maxHealth
local dam = damage
health.Value -= dam
local inventory = require(game.ReplicatedStorage.Tables.ItemValues.Inventory)
if game.ReplicatedStorage.Tools:FindFirstChild(item) then
elseif item.Parent.Name == "Rocks" then
local special = item.Container.specialChance
local min = item.Container.minAmount
local maxA = item.Container.maxAmount
print(health)
if health.Value >= (max.Value/2) and health.Value > 0 and item.LargeRock.Transparency == 0 then
if detectSide() == true then
game.SoundService.SFX.LargeRock:Play()
end
if item.Name == "Stone" then
local special2 = math.random(1, special.Value)
local amount = math.random(min.Value, max.Value)
if special2 == (special.Value/2) then
inventory:AddItem(player, "Special", 1, detectSide())
end
inventory:AddItem(player, "Stone", amount, detectSide())
end
item.LargeRock.Transparency = 1
item.LargeRock.CanCollide = false
item.LargeRock.Size = Vector3.new(1,1,1)
item.PrimaryPart = item.SmallRock
item.SmallRock.Transparency = 0
item.SmallRock.CanCollide = true
elseif health.Value <= 0 then
if detectSide() == true then
game.SoundService.SFX.SmallRock:Play()
end
if item.Name == "Stone" then
local special2 = math.random(1, special.Value)
local amount = math.random(min.Value, max.Value)
if special2 == (special.Value/2) then
inventory:AddItem(player, "Special", 1, detectSide())
end
inventory:AddItem(player, "Stone", amount, detectSide())
end
item:Destroy()
end
end
end
return custom