Hi i want help with my script

So i have an mining script when i activate my tool it fires an event (The event is stored in the tool), and damages the part im mining
after the part’s health is 0 the part becomes transparency 1, cancollide = false. But after the part respawns i can mine the part even without touching it please help, (The part respawns by setting the transparency to 0 and cancollide to true)
`
if hit.Parent:IsA(“Tool”) and hit.Parent.Name == “Pickaxe” then

	hit.Parent.Mine.OnServerEvent:Connect(function()
		if script.Parent.Transparency == 0 then
			script.Parent.Value.Value = script.Parent.Value.Value - 1		
		end
	end)

`

Could we see the localscript that fires the event?

local anim = script:WaitForChild(“Animation”)
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local hum = char:WaitForChild(“Humanoid”)

local MineAnim = hum:LoadAnimation(anim)
local MineEvent = script.Parent.Mine

local tool = script.Parent
local debounce = false

script.Parent.Equipped:Connect(function(Mouse)

Mouse.Button1Down:Connect(function()
	if debounce == false then
		debounce = true
		
	MineAnim:Play()
	MineEvent:FireServer()
	tool.Handle.Sound:Play()
		
		wait(.3)
		debounce = false			
	end
end)

end)

also i didnt mention the mining detection script is inside of the part

It doesn’t look like you formatted your code correctly, and it also looks like you are missing a little bit of code. Could you retry formatting it please?

It doesn’t miss any code
it’s maybe a little hard to understand my code.

local anim = script:WaitForChild("Animation")
local char = game.Players.LocalPlayer.Character or 
game.Players.LocalPlayer.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local MineAnim = hum:LoadAnimation(anim)
local MineEvent = script.Parent.Mine

local tool = script.Parent
local debounce = false

    script.Parent.Equipped:Connect(function(Mouse)

Mouse.Button1Down:Connect(function()
	if debounce == false then
		debounce = true
		
	MineAnim:Play()
	MineEvent:FireServer()
	tool.Handle.Sound:Play()
		
		wait(.3)
		debounce = false			
	end
 end)
 end)

Could I see the first script but formatted correctly?

The script checks if a pickaxe touches the part (This script is inside of the part not the pickaxe) i have multiple pickaxes

 script.Parent.Touched:connect(function(hit)

if hit.Parent:IsA("Tool") and hit.Parent.Name == "Pickaxe" then	
	
	hit.Parent.Mine.OnServerEvent:Connect(function()
		if script.Parent.Transparency == 0 then
			script.Parent.Value.Value = script.Parent.Value.Value - 1		
		end
	end)
	
elseif hit.Parent:IsA("Tool") and hit.Parent.Name == "Pickaxe2" then
	hit.Parent.Mine.OnServerEvent:Connect(function()
		if script.Parent.Transparency == 0 then
			script.Parent.Value.Value = script.Parent.Value.Value - 4				
		end
	end)
	
elseif hit.Parent:IsA("Tool") and hit.Parent.Name == "Pickaxe3" then
	hit.Parent.Mine.OnServerEvent:Connect(function()
		if script.Parent.Transparency == 0 then
			script.Parent.Value.Value = script.Parent.Value.Value - 8					
		end
	end)
	
elseif hit.Parent:IsA("Tool") and hit.Parent.Name == "Pickaxe4" then
	hit.Parent.Mine.OnServerEvent:Connect(function()
		if script.Parent.Transparency == 0 then
			script.Parent.Value.Value = script.Parent.Value.Value - 16				
		end
	end)
	
elseif hit.Parent:IsA("Tool") and hit.Parent.Name == "Pickaxe5" then	
	hit.Parent.Mine.OnServerEvent:Connect(function()
		if script.Parent.Transparency == 0 then
			script.Parent.Value.Value = script.Parent.Value.Value - 32				
		end
	end)
	
elseif hit.Parent:IsA("Tool") and hit.Parent.Name == "Pickaxe6" then	
	hit.Parent.Mine.OnServerEvent:Connect(function()
		if script.Parent.Transparency == 0 then
			script.Parent.Value.Value = script.Parent.Value.Value - 64				
		end
	end)
	
elseif hit.Parent:IsA("Tool") and hit.Parent.Name == "Pickaxe7" then
	hit.Parent.Mine.OnServerEvent:Connect(function()
		if script.Parent.Transparency == 0 then
			script.Parent.Value.Value = script.Parent.Value.Value - 128					
		end
	end)
	
elseif hit.Parent:IsA("Tool") and hit.Parent.Name == "Pickaxe8" then
	hit.Parent.Mine.OnServerEvent:Connect(function()
		if script.Parent.Transparency == 0 then
			script.Parent.Value.Value = script.Parent.Value.Value - 256					
		end
	end)
	
elseif hit.Parent:IsA("Tool") and hit.Parent.Name == "Pickaxe9" then
	hit.Parent.Mine.OnServerEvent:Connect(function()
		if script.Parent.Transparency == 0 then
			script.Parent.Value.Value = script.Parent.Value.Value - 512				
		end
	end)
	
elseif hit.Parent:IsA("Tool") and hit.Parent.Name == "Pickaxe10" then	
	hit.Parent.Mine.OnServerEvent:Connect(function()
		if script.Parent.Transparency == 0 then
			script.Parent.Value.Value = script.Parent.Value.Value - 1024					
		end
	end)
	
elseif hit.Parent:IsA("Tool") and hit.Parent.Name == "Pickaxe11" then	
	hit.Parent.Mine.OnServerEvent:Connect(function()
		if script.Parent.Transparency == 0 then
			script.Parent.Value.Value = script.Parent.Value.Value - 2048			
		end
	end)
	
elseif hit.Parent:IsA("Tool") and hit.Parent.Name == "Pickaxe13" then
	hit.Parent.Mine.OnServerEvent:Connect(function()
		if script.Parent.Transparency == 0 then
			script.Parent.Value.Value = script.Parent.Value.Value - 4096				
		end
	end)
	
end
 end)

do you know what for loops are?
this is a lot of repeated code

I know what for loops are but i dont know how would i use them in this script…
even if there was for loops it wouldn’t fix my problem

I didn’t say it would fix the problem, just saying that code has so much repeated code

You are grossly misusing RemoteEvents in this situation. Every time your part touches another part, it will go through all those if statements and then create a brand new event that listens for a RemoteEvent call. If you aren’t clearing those events, then they will continue to listen and fire their relevant code when you do not expect them to.

then what should i do? im not the best scripter

If you describe exactly what you’re trying to accomplish, I can point you in the right direction. Just from looking at the code you’ve provided, I am a little confused as to how you’re going about creating a mining system for your game.

1 Like

It works like this, inside of every ore there is a script that clones a pickaxe detection script from the replicated storage to the ore, the script checks if the remote event in the pickaxe fires then the ore will take damage… but after i mine the ore and it respawns, i can mine it from whatever distance i want, but i dont want that…

Instead of having each ore create a listener for a remote event, why not have one single script that listens for the remote event and is sent information, such as the ore the player wants to mine. That script will confirm that the ore is able to be mined and the player is close enough to mine it, then damage that ore.

You could do something like

RemoteEvent.OnServerEvent:Connect(function(player, ore)
	local pickaxe = player.Character:FindFirstChildWhichIsA("Tool") -- find the pickaxe that the player is holding
	
	if pickaxe then
		local pickaxeDamge = -- get the correct damage for their pickaxe
		
		for _,v in pairs (OreObjects:GetChildren()) do -- iterate through all the active ore objects
			if v == ore and player:DistanceFromCharacter(ore.Position) <= 15 then 
				-- did the player send a valid ore? are they close enough to it?
				
				if ore.Value.Value >= 0 then -- depending on how you handle respawning, this might not be needed
					ore.Value.Value -= pickaxeDamage
					
					if ore.Value.Value <= 0 then
						-- handle ore respawning here as well as giving the player what they should get
					end
				end
				
				return
			end
		end
	end
end)

I left a few things unanswered, and you may have to change some things around, but this is just an example of how I would tackle this problem.

This worked thanks!

ore = script.Parent

 script.Parent.Touched:connect(function(hit)
if hit.Parent:IsA("Tool") and hit.Parent.Name == "Pickaxe" then	
	local debounce = false

	hit.Parent.Mine.OnServerEvent:Connect(function(player)
					if script.Parent.Transparency == 0 and player:DistanceFromCharacter(ore.Position) <= 15 then
			if debounce == false then
				debounce = true
				local Details = script.Parent.Details:GetChildren()
				for i = 1, #Details do
					local detail = Details[i]
					detail.Size = Vector3.new(1.5 ,1.5 ,1.5)
				end

				script.Parent.Value.Value = script.Parent.Value.Value - 1

				wait(.3)

				for i = 1, #Details do
					local detail = Details[i]
					detail.Size = Vector3.new(1.8 ,1.8 ,1.8)
				end

				debounce = false
			end
		end
	end)
end