2

i am currently creating a mining game and im trying to add breakable blocks. so im trying to create a script when broken put ruby into StarterPack. my files:

Ruby(part):
   Hit(click)
   ParticleEmitter(particle emitter)
   ClickDetector(clickdetector):
      Script(script)
      Ruby(tool)
StarterPack: pickaxe(tool): (not a part of the script)

script:

local debounce = false

-- settings
local cooldown = 0.3
local hp = 5
local particleduration = 0.3

--functions
local function particle()  --function for the particle
    particlee.Enabled = true
    wait(particleduration)
    particlee.Enabled = false
end

--main code
function onMouseClick() --detect if player clicked on part
    if debounce == false then  --check for cooldown
        debounce = true --start cooldown
        particle() --spawn particles
        hitcount.Value += 1 -- damage block
        if hitcount.Value == hp then --check if block is damaged enought to break
            block:Destroy() --destroy the block
        end
        wait(cooldown)  --cooldown
        debounce = false --remove cooldown
    end
    
end

clickDetector.MouseClick:connect(onMouseClick)
gleacc
  • 78
  • 9
  • Are you sure you want to send the Tool to the StarterPack? It won't show up in their Backpack until the next time they spawn. – Kylaaa Oct 17 '22 at 00:25
  • im trying to make this like when they break the block the item goes into the inventory – gleacc Oct 22 '22 at 01:37

2 Answers2

0

You can get the player that interacted with the ClickDetector from the MouseClick event and simply clone the tool from there.

function onMouseClick(player) --detect if player clicked on part
    if debounce == false then  --check for cooldown
        debounce = true --start cooldown
        particle() --spawn particles
        hitcount.Value += 1 -- damage block
        if hitcount.Value >= hp then --check if block is damaged enough to break
            block:Destroy() --destroy the block
    
            local tool = script.Parent.Ruby:Clone() -- find the tool to clone
            local target = game:GetService("StarterPack") -- find where we're moving the tool to
            tool.Parent = target -- add the newly created tool into StarterPack
        end
        wait(cooldown)  --cooldown
        debounce = false --remove cooldown
    end
    
end

clickDetector.MouseClick:connect(onMouseClick)

As a side note, it's unusual to move things into StarterPack though. Objects there are only cloned into a player's Backpack when they spawn. So in this scenario, the player that harvested the Ruby wouldn't necessarily get it, it would go into a communal pool that everyone would have access to (which could be intentional, I dunno). But if you want to give the player that broke the block the Ruby, just put it into their Backpack. And you can do that by changing the target line to this :

local target = player.Backpack -- find where we're moving the tool to
Kylaaa
  • 6,349
  • 2
  • 16
  • 27
0

I think you should make all the tools and put them in a folder in the Replicated Storage. The folder needs to be named 'BlocksMined'

After you done all this, create a script in the blocks you can mine and add a ClickDetector. In the script, type all of that below.

local CD = script.Parent.ClickDetector
local BM = game.ReplicatedStorage.BlocksMined

CD.MouseClick:Connect(function(plr)
    BM.Stone:Clone().Parent = plr.Backpack -- Only I think that it is Stone, Change it to anything else
    script.Parent:Destroy()
end)

I tested it in game before and it worked, everything here are free to copy.

if you want to make the block to be damaged before getting it, copy the one down there in the script.

But you need to prepare 1 more thing. add a IntValue in the blocks. Name it 'Health'. For me, I will make the health to be 20. every click -5.

local CD = script.Parent.ClickDetector
local BM = game.ReplicatedStorage.BlocksMined

CD.MouseClick:Connect(function(plr)
    if script.Parent.Health.Value > 0 then
        script.Parent.Health.Value = script.Parent.Health.Value - 5
        print(script.Parent.Health.Value) -- To let you know to remaining health of the block, you can display it in a gui bar later.
    else
        BM.Stone:Clone().Parent = plr.Backpack 
        script.Parent:Destroy()
    end
end)

I also tested it and it worked, tell me if you want to make the gui bar.