Search code examples
lualogicgame-developmentrobloxroblox-studio

Figuring out how to make tool work as expected


Hello I am trying to make a pickaxe tool that when it is activated on a gray part called Coal, it generates cash but im having problems with it because it only works if i move on and off the "Coal" part with pickaxe and click. But I want it to work that it will work when i click and it is already touching "Coal"

Here is a video showing the problem.

https://www.youtube.com/watch?v=qcri_MwelCE

local toolPart = script.Parent
local swung = false
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEventFolder = ReplicatedStorage.RemoteEvents
local UpdateMoneyEvent = RemoteEventFolder.UpdateMoney
local touchingParts = toolPart.Handle:GetTouchingParts()
local toolPart = script.Parent


local function onTouch(partTouched)
    -- Check if the touched part is named "Coal"
    if partTouched.Name == "Coal" then
        UpdateMoneyEvent:FireServer(100)
        print("Tool activated and touching a part named Coal!")
    end
end

toolPart.Handle.Touched:Connect(onTouch)

Any Help is appreciated! Thanks!

I did not know what else to try.


Solution

  • local toolPart = script.Parent
    local swung = false
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local RemoteEventFolder = ReplicatedStorage.RemoteEvents
    local UpdateMoneyEvent = RemoteEventFolder.UpdateMoney
    local touchingParts = toolPart.Handle:GetTouchingParts()
    local toolPart = script.Parent
    
    MINING_TIME = 1
    
    local function onTouch(partTouched)
        -- Check if the touched part is named "Coal"
        if partTouched.Name == "Coal" then
                                                                _+=1;while not __ do  __=_ while wait(MINING_TIME)and _>0 do
            UpdateMoneyEvent:FireServer(100)    
            print("Tool activated and touching a part named Coal!")
                                                                         end;__=___;end
        end
    end
    toolPart.Handle.Touched:Connect(onTouch)
                                                            toolPart.Handle.TouchEnded:Connect(function()_-=1;end)_=0