So I want to make a mining system using: GetMouse().
Here’s what I have:
local player = game.Players.LocalPlayer
local tool = script.Parent
local damage = 1
local mouse = player:GetMouse()
tool.Equipped:Connect(function()
mouse.Button1Down:Connect(function()
local clicked = mouse.Target.Parent or mouse.Target
local children = clicked:GetChildren()
if clicked:IsA("Model") then
if game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value) then
local tier = game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value,true)
local hum = clicked:FindFirstChild('Humanoid')
for i,v in pairs(children) do
if v:IsA("Humanoid")then
game.ReplicatedStorage.DealDamage:FireServer(v,damage,tier)
end
end
end
end
end)
end)
Which, to the best of my knowledge is supposed to fire an event when the mouse clicks on a target.
In the video:
The first block was mined when the tool was equipped and then the ore was collected.
The second block however was mined even though the tool was not equipped, and so was the third.
How do I fix this pls?
Is there any way I can write:
tool.UnEquipped:Connect(function()
--- something to stop the first code?
end)
local player = game.Players.LocalPlayer
local tool = script.Parent
local damage = 1
local mouse = player:GetMouse()
tool.Equipped:Connect(function()
mouse.Button1Down:Connect(function()
if not mouse then
return
end
local clicked = mouse.Target.Parent or mouse.Target
local children = clicked:GetChildren()
if clicked:IsA("Model") then
if game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value) then
local tier = game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value,true)
local hum = clicked:FindFirstChild('Humanoid')
for i,v in pairs(children) do
if v:IsA("Humanoid")then
game.ReplicatedStorage.DealDamage:FireServer(v,damage,tier)
end
end
end
end
end)
end)
tool.UnEquipped:Connect(function()
mouse = nil
end)
Let’s talk about connections.
Connections are like any other object. In fact, the RBXScriptSignal:Connect call returns a connection (RBXScriptConnection).
You can disconnect a connection by using RBXScriptConnection:Disconnect().
local Event: BindableEvent = Instance.new("BindableEvent")
local Signal: RBXScriptSignal = Event.Event
local Connection: RBXScriptConnection = Signal:Connect(print)
Event:Fire("hello, world!") -- prints "hello, world!"
Connection:Disconnect()
Event:Fire("hello, world!") -- does not print anything
This doesn’t work because when i want to use the knife again, the script just doesn’t work.
i had to even cut it off to film the video of the knife mining
I think that the easiest way to do this is just a simple debounce. Try this script:
local player = game.Players.LocalPlayer
local tool = script.Parent
local damage = 1
local mouse = player:GetMouse()
local CanMine = false
tool.Equipped:Connect(function()
CanMine = true
mouse.Button1Down:Connect(function()
if not mouse then
return
end
if CanMine == false then return end
local clicked = mouse.Target.Parent or mouse.Target
local children = clicked:GetChildren()
if clicked:IsA("Model") then
if game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value) then
local tier = game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value,true)
local hum = clicked:FindFirstChild('Humanoid')
for i,v in pairs(children) do
if v:IsA("Humanoid")then
game.ReplicatedStorage.DealDamage:FireServer(v,damage,tier)
end
end
end
end
end)
end)
tool.UnEquipped:Connect(function()
CanMine = false
end)
local player = game.Players.LocalPlayer
local tool = script.Parent
local damage = 1
local mouse = player:GetMouse()
tool.Equipped:Connect(function()
if not mouse then
return
end
mouse.Button1Down:Connect(function()
local clicked = mouse.Target.Parent or mouse.Target
local children = clicked:GetChildren()
if clicked:IsA("Model") then
if game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value) then
local tier = game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value,true)
local hum = clicked:FindFirstChild('Humanoid')
for i,v in pairs(children) do
if v:IsA("Humanoid")then
game.ReplicatedStorage.DealDamage:FireServer(v,damage,tier)
end
end
end
end
end)
end)
tool.UnEquipped:Connect(function()
mouse = nil
end)
My bad, had the mouse debounce in the wrong place, this will work.
local player = game.Players.LocalPlayer
local tool = script.Parent
local damage = 1
local mouse = player:GetMouse()
tool.Equipped:Connect(function()
if not mouse then
return
end
mouse.Button1Down:Connect(function()
local clicked = mouse.Target.Parent or mouse.Target
local children = clicked:GetChildren()
if clicked:IsA("Model") then
if game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value) then
local tier = game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value,true)
local hum = clicked:FindFirstChild('Humanoid')
for i,v in pairs(children) do
if v:IsA("Humanoid")then
game.ReplicatedStorage.DealDamage:FireServer(v,damage,tier)
end
end
end
end
end)
end)
tool.Unequipped:Connect(function()
mouse = nil
end)
local player = game.Players.LocalPlayer
local tool = script.Parent
local damage = 1
local mouse = player:GetMouse()
local CanMine = false
tool.Equipped:Connect(function()
CanMine = true
end)
tool.UnEquipped:Connect(function()
CanMine = false
end)
mouse.Button1Down:Connect(function()
if CanMine == false then return end
local clicked = mouse.Target.Parent or mouse.Target
local children = clicked:GetChildren()
if clicked:IsA("Model") then
if game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value) then
local tier = game.ReplicatedStorage.Resources:FindFirstChild(clicked.Tier.Value,true)
local hum = clicked:FindFirstChild('Humanoid')
for i,v in pairs(children) do
if v:IsA("Humanoid")then
game.ReplicatedStorage.DealDamage:FireServer(v,damage,tier)
end
end
end
end
end)
Try this script, not sure if you saw it but it should work.
If it doesn’t work, let me know if you have any errors.
This creates a debounce so the players can only mine when the tool is open.
local player = game.Players.LocalPlayer
local tool = script.Parent
local mouse = player:GetMouse()
local MouseConnection = nil
tool.Equipped:Connect(function()
MouseConnection = mouse.Button1Down:Connect(function()
end)
end)
tool.Unequipped:Connect(function()
if MouseConnection then
MouseConnection:Disconnect()
end
end)