Hey! I’m working on a mining system for a game and was wondering what I could improve and what could be easily exploitable.
Here is how the tool is structured:
(i really don’t know if i should be storing the event in ReplicatedStorage or the tool )
Here is the code:
Server:
-- Variables
local pickaxe = script.Parent
local mineEvent = pickaxe:WaitForChild("Mine")
-- Main code
mineEvent.OnServerEvent:Connect(function(player, mouseTarget)
local health = mouseTarget.Parent:FindFirstChild("Health")
health.Value = health.Value - 1
if health.Value <= 0 then
mouseTarget.Parent:Destroy()
end
end)
Client:
-- Services
local Players = game:GetService("Players")
-- Variables
local pickaxe = script.Parent
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local mineEvent = pickaxe:WaitForChild("Mine")
local mining = false
-- Main code
pickaxe.Activated:Connect(function()
if mining == false then
mining = true
local target = mouse.Target
if target then
local distanceFromTarget = (humanoidRootPart.Position - target.Position).Magnitude
if distanceFromTarget < 10 then
local health = target.Parent:FindFirstChild("Health")
if health then
mineEvent:FireServer(target)
end
end
end
task.wait(1)
mining = false
end
end)