Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Damages goes through 2 or more times. How to fix it?

Asked by 1 year ago

Hello, I made a script that removes health from the stone and finds the damage of your pickaxe. I made a stone respawn, and when stone respawns, the damage goes through two or more times and I don’t know what to do.

ToolScript1 =

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local collect = game:GetService("CollectionService")
local Players = game:GetService("Players")

local Tool = script.Parent
local Remotes = ReplicatedStorage:FindFirstChild("Remotes")

local player = Players.LocalPlayer
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")
local PickaxeSwingAnim = Instance.new("Animation")
PickaxeSwingAnim.AnimationId = "rbxassetid://11458130668"
local animationTrack1 = humanoid:LoadAnimation(PickaxeSwingAnim)

local ToolConfig = require(ReplicatedStorage:FindFirstChild("Config"):FindFirstChild("ToolConfig"))
local OreConfig = require(ReplicatedStorage:FindFirstChild("Config"):FindFirstChild("OreConfig"))


local inventory = player:FindFirstChild("inventory")
local pickaxeName2 = Tool.Name
print("Found", player.Name, "Pickaxe Equipped Name!")
local pickaxe = player.Character:FindFirstChild(pickaxeName2)
print (pickaxeName2)
local pickaxeDmg = ToolConfig[pickaxeName2].Damage
local Ore = player.leaderstats.Ore

Tool.Activated:Connect(function(player: Player)
    if Tool.Enabled == true then
        local punch = false
        for tool, toolTable in pairs(ToolConfig) do
            if tool == pickaxeName2 then
                local Lezvie = script.Parent.Handle.Lezvie
                Remotes.ToolActivated:FireServer()
                animationTrack1:Play()
                Tool.Enabled = false
                humanoid.WalkSpeed = 0
                wait(0.75)
                Tool.Enabled = true
                humanoid.WalkSpeed = 16
                Lezvie.Touched:Connect(function(hit)
                    if collect:HasTag(hit, "RockHitbox") then
                        if punch == false then
                                Remotes.OreDamaged:FireServer()
                        end
                        punch = true
                    end
                end)

                punch = false
            end
        end
    end
end)




Remotes.OreDestroyed.OnClientEvent:Connect(function()
    Remotes.OreDestroyed:FireServer()
end)
Remotes.OreRespawn.OnClientEvent:Connect(function()
    Remotes.OreRespawn:FireServer()
end)

Rock script:

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Remotes = ReplicatedStorage:FindFirstChild("Remotes")
local ToolConfig = require(ReplicatedStorage:FindFirstChild("Config"):FindFirstChild("ToolConfig"))
local OreConfig = require(ReplicatedStorage:FindFirstChild("Config"):FindFirstChild("OreConfig"))
local OreName = script.Parent.Name
local Ore = script.Parent
print(OreName)

local miningSound = Instance.new("Sound", Ore)
miningSound.SoundId = "rbxassetid://5772362759"
miningSound.Name = "MiningSound"

local OreDestroyed = false




Players.PlayerAdded:Connect(function()
    if not OreName.Health then
        local HP = Instance.new("IntValue", script.Parent)
        HP.Name = "Health"
        HP.Value = OreConfig[OreName].Health
    end
end)





Remotes.OreDamaged.OnServerEvent:Connect(function(player: Player)
    local OreL = player.leaderstats.Ore
    local pickaxeName = player.inventory.EquippedTool.Value
    local pickaxeDamage = ToolConfig[pickaxeName].Damage
    print(pickaxeDamage)
    local Tool = player.Character:FindFirstChild(pickaxeName)
    if OreDestroyed == false then
        local HP = script.Parent.Health
        print("02")
        HP.Value -= pickaxeDamage
        OreL.Value += pickaxeDamage
        print("02.1")
        miningSound:Play()
        print(HP.Value, "Rock HP.")
        if HP.Value == 0 then
            Remotes.OreDestroyed:FireClient(player)
        end
    end
end)




Remotes.OreDestroyed.OnServerEvent:Connect(function(player: Player)
    OreDestroyed = true
    Ore.Transparency = 1
    Ore.CanCollide = false
    Ore.CanTouch = false
    Ore.Hitbox.CanCollide = false
    Ore.Hitbox.CanTouch = false
    Remotes.OreRespawn:FireClient(player)
end)



Remotes.OreRespawn.OnServerEvent:Connect(function(player: player)
    local RespawnTime = 1
    wait(RespawnTime)
    OreDestroyed = false
    Ore.Transparency = 0
    Ore.CanCollide = true   
    Ore.CanTouch = true
    Ore.Hitbox.CanCollide = true
    Ore.Hitbox.CanTouch = true
    print("01")
    local Health = script.Parent.Health
    Health.Value = OreConfig[OreName].Health
    print("Rock health after RESPAWN:", Health.Value)
end)

Output:

  22:36:19.255  0 Rock HP.  -  Server - OreScript:46
  22:36:19.871  OsipStar4ik Used Pickaxe  -  Server - toolManger:13
  22:36:20.338  01  -  Server - OreScript:77
  22:36:20.338  Rock health after RESPAWN: 10  -  Server - OreScript:80
  22:36:22.687  OsipStar4ik Used Pickaxe  -  Server - toolManger:13
  22:36:22.954  1  -  Server - OreScript:37
  22:36:22.955  02  -  Server - OreScript:41
  22:36:22.955  02.1  -  Server - OreScript:44
  22:36:22.955  9 Rock HP.  -  Server - OreScript:46
  22:36:22.955  1  -  Server - OreScript:37
  22:36:22.955  02  -  Server - OreScript:41
  22:36:22.955  02.1  -  Server - OreScript:44
  22:36:22.955  8 Rock HP.  -  Server - OreScript:46

1 answer

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

It seems that what is happening in your code is that each time you swing your pickaxe, the code starts listening for touched events. The problem is simply that it doesn't stop listening for touches, so when you swing your pickaxe again, it will fire twice.

So, all you have to do is separate the two something like this:

Tool.Activated:Connect(function(player: Player)
    if Tool.Enabled == true then
        for tool, toolTable in pairs(ToolConfig) do
            if tool == pickaxeName2 then
                Remotes.ToolActivated:FireServer()
                animationTrack1:Play()
                active = true
                Tool.Enabled = false
                humanoid.WalkSpeed = 0
                wait(0.75)
                Tool.Enabled = true
active = false
                humanoid.WalkSpeed = 16
            end
        end
    end
end)

local Lezvie = script.Parent.Handle.Lezvie
punch = false
Lezvie.Touched:Connect(function(hit)
    if punch == false then
        punch = true
        if active == true then
            if collect:HasTag(hit, "RockHitbox") then
                Remotes.OreDamaged:FireServer()
            end
        end
        punch = false
    end
end)

(EDIT FOR CLARITY)

This code is for Toolscript1, and replaces the code that was

Tool.Activated:Connect(function(player: Player)
    if Tool.Enabled == true then
...
end)

from lines 27 to 53 in the original code.

Ad

Answer this question