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

When I destroy ore, script destroys all ores. How to fix it?

Asked by 2 years ago

Hello, I made a script that damages the ore and the ore, if its HP = 0, then it becomes invisible. I had the following bug: When I break one stone, all other ores break as well

Tool Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local collect = game:GetService("CollectionService")
local StarterGui = game:GetService("StarterGui")
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
        print("Tool.Enabled == true")
        for tool, toolTable in pairs(ToolConfig) do
            if tool == pickaxeName2 then
                print(pickaxeName2)
                Remotes.ToolActivated:FireServer()
                animationTrack1:Play()
                active = true
                Tool.Enabled = false
                game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
                humanoid.WalkSpeed = 0
                wait(0.75)
                Tool.Enabled = true
                active = false
                humanoid.WalkSpeed = 16
                game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
                print("End")
            end
        end
    end
end)

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




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

Ore 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 FailMiningSound = Instance.new("Sound", Ore)
FailMiningSound.SoundId = "rbxassetid://9119510368"
FailMiningSound.Name = "FailSound"

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
    local pickaxeSila = ToolConfig[pickaxeName].Sila
    local oreSila = OreConfig[OreName].Sila
    local Tool = player.Character:FindFirstChild(pickaxeName)
    local HP = script.Parent.Health
    print(pickaxeSila)
    print(oreSila)
    if OreDestroyed == false then
        print("101")
        --if oreSila == pickaxeSila or oreSila <= pickaxeSila then
        HP.Value -= pickaxeDamage
        OreL.Value += pickaxeDamage
        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 = OreConfig[OreName].RespawnTime
    wait(RespawnTime)
    OreDestroyed = false
    Ore.Transparency = 0
    Ore.CanCollide = true   
    Ore.CanTouch = true
    Ore.Hitbox.CanCollide = true
    Ore.Hitbox.CanTouch = true
    local Health = script.Parent.Health
    Health.Value = OreConfig[OreName].Health
    print(OreName, "Health after RESPAWN:", Health.Value)
end)

1 answer

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

Your code does not tell which ore is getting destroyed. It just sends a signal that an ore has been destroyed, and all the ore scripts get that signal, and think that their ore has been destroyed.

To fix this, all you need to do is tell the ores which one has been destroyed.

ToolScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local collect = game:GetService("CollectionService")
local StarterGui = game:GetService("StarterGui")
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
        print("Tool.Enabled == true")
        for tool, toolTable in pairs(ToolConfig) do
            if tool == pickaxeName2 then
                print(pickaxeName2)
                Remotes.ToolActivated:FireServer()
                animationTrack1:Play()
                active = true
                Tool.Enabled = false
                game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
                humanoid.WalkSpeed = 0
                wait(0.75)
                Tool.Enabled = true
                active = false
                humanoid.WalkSpeed = 16
                game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
                print("End")
            end
        end
    end
end)

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




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

Ore 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 FailMiningSound = Instance.new("Sound", Ore)
FailMiningSound.SoundId = "rbxassetid://9119510368"
FailMiningSound.Name = "FailSound"

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
    local pickaxeSila = ToolConfig[pickaxeName].Sila
    local oreSila = OreConfig[OreName].Sila
    local Tool = player.Character:FindFirstChild(pickaxeName)
    local HP = script.Parent.Health
    print(pickaxeSila)
    print(oreSila)
    if OreDestroyed == false then
        print("101")
        --if oreSila == pickaxeSila or oreSila <= pickaxeSila then
        HP.Value -= pickaxeDamage
        OreL.Value += pickaxeDamage
        miningSound:Play()
        print(HP.Value, "Rock HP.")
        if HP.Value <= 0 then
            Remotes.OreDestroyed:FireClient(player,OreL)
        end
    end
end)




Remotes.OreDestroyed.OnServerEvent:Connect(function(player: Player, ore)
    if ore == script.Parent then
        OreDestroyed = true
        Ore.Transparency = 1
        Ore.CanCollide = false
        Ore.CanTouch = false
        Ore.Hitbox.CanCollide = false
        Ore.Hitbox.CanTouch = false
        Remotes.OreRespawn:FireClient(player, ore)
    end
end)



Remotes.OreRespawn.OnServerEvent:Connect(function(player: player, ore)
    if ore == script.Parent then
        local RespawnTime = OreConfig[OreName].RespawnTime
            wait(RespawnTime)
        OreDestroyed = false
        Ore.Transparency = 0
        Ore.CanCollide = true   
        Ore.CanTouch = true
        Ore.Hitbox.CanCollide = true
        Ore.Hitbox.CanTouch = true
        local Health = script.Parent.Health
        Health.Value = OreConfig[OreName].Health
        print(OreName, "Health after RESPAWN:", Health.Value)
    end
end)
0
Hello! I was only able to check the script today, now when I hit a stone, health can go in minus, and the damage still passes through two stones at once OsipStar4ik 32 — 2y
0
And now the stones unbreakable OsipStar4ik 32 — 2y
0
that... is weird. sergeant_ranger 184 — 2y
0
one problem is that it only checks if the health is exactly 0, which it wont always be sergeant_ranger 184 — 2y
0
I have edited my answer to fix that, as well as hopefully fixing the damage duplicating through sergeant_ranger 184 — 2y
Ad

Answer this question