Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How Would I Make This Digging Script Work Properly?

Hey guys, I am trying to make a digging game where if you click a diggable part, it will slowly reduce the size of the part until it is completely gone and when it is gone it will give you coins in return. I have a script that works but for some reason when you walk away from the part, you are still able to dig it even when you are not touching it. Anyone know how I am fix this?

Script:

--Script in ServerScriptService

ce.OnServerEvent:Connect(function(plr, part)

part.Size = part.Size - Vector3.new(.01,.01,.01)

end)

--Script in tool

script.Parent.ShovelTip.Touched:Connect(function(hitpart)

mouse.Button1Down:Connect(function()

if hitpart.Name == "Dirt" then

local part = hitpart

harvesting.Value = true

ce:FireServer(part)


end

end)

end)

1 comment

0

The reason is because you need to use the Remove() Function. This way it would decrease lag as well because you wouldn't have a million parts in explorer.

1 answer

You have to check for the distance between the LocalPlayer and the current Part To Dig by using Magnitude like in the following.

The script that is going to be used is LocalScript

Getting the Distance between Player and Part. Code:

--Variables To be Used--
local part = script.Parent --Getting Part
local player = game.Players.LocalPlayer --Getting The LocalPlayer
player.CharacterAdded:Wait() --Waiting for the player's Character
local char = player.Character --Getting player's Character

spawn(function() --Threading the while loop, so it doesn't crash
	local TO = char:WaitForChild("Torso")
	while true do
		local dist = (TO.Position - part.Position).magnitude
		if dist <= 6 then --If distance between player and part is 6  studs then
			print("Player is close and able to dig!")
			<RemoteEvent>:FireServer() --Firing remote to start digging
		end
		wait()
	end
end)

And this should be it for checking if player is close or not, this will automaticly dig, you can just modify it a bit.

Hope This Helped.

This discussion is preserved as it was. New answers, comments, and votes are closed.