I am creating a game and having issues with a shovel I am creating.
I want the Shovel to only run the dig function if it detects the player inside a part/region
The part name is an attribute inside the shovel tool, The part is in workspace.
Here is the full script:
local MouseData = Instance.new(“RemoteFunction”,script.Parent)
local KeyEvent = Instance.new(“RemoteEvent”,script.Parent)
local Tool = script.Parent
local Gui = script.SoilGUI
local Handle = Tool.Handle
local attribute = script.Parent.Configuration
local Attrib = script.Parent.Configuration
local digpart = game.Workspace:FindFirstChild(Attrib:GetAttribute(“Dig_Region_Block_Name”))
local Hitting = false
script.Parent.Activated:Connect(function()
if not Tool.Enabled then return end
Swing()
local Player = game.Players:GetPlayerFromCharacter(Tool.Parent)
if Player.Character.Humanoid.Health <= 0 then return end
if Player then
local Target,HitPos = MouseData:InvokeClient(Player)
if Target and (HitPos - Handle.Position).Magnitude <= Attrib:GetAttribute(“ReachDistance”) then
if Target == workspace.Terrain then
if digpart:GetPartsInPart(Player.humanoid) then
Tool.Enabled = false
CreateDust(HitPos)
workspace.Terrain:FillBall(HitPos,Attrib:GetAttribute(“DigSize”) ,Enum.Material.Air)
Handle.Dig:Play()
if Attrib:GetAttribute(“ConsumeSoil”) then
Attrib:SetAttribute(“Soil”, Attrib:GetAttribute(“Soil”) + 1)
end
if Attrib:GetAttribute(“GuiEnabled”) then
KeyEvent:FireClient(Player,Gui,Attrib:GetAttribute(“Soil”))
end
wait(Attrib:GetAttribute(“Cooldown”))
Tool.Enabled = true
end
end
end
end
end)
Ok so here’s what I deduce. The configuration is in startergui, its parent is a local script which is also the parent of the script you’re showing here. Server Scripts cannot work in StarterGui.
Move this script to somewhere else where a server script can work (SSS, Workspace) and write this instead
local digpart = game.Workspace:FindFirstChild(game.StarterGui.Tool.LocalScript.Configuration:GetAttribute("Dig_Region_Block_Name")) -- Change this to your tool and the local script in it
while task.wait(0.5) do
print(digpart.Position)
end
okay so what might be happening is that the checks before the dig area check might not be passing so the dig area check does not run
THE OTHER problem might be that since this runs on a local script it doesnt wait before the dig area is loaded (but this is unlikely because it would probably error if its trying to check the position of nil but it doesnt hurt to just make it WaitForChild either)