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)
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.