-
i’m trying to make it to where whenever the player touches the script.parent brick then give them owner to the area/zone to build on and to give them tools to build with basically. but making the zone/area to build on be the script.Parent
-
the issue is the owner is not receiving their tools when they should.
-
i tried many options and ways changing how it detects owner and other things, i just can’t do anything at this point
i know the code is a bit messy and i know i added kick lol you can change that to better if you want to i don’t mind i just need help with the main issue, this is just a foundation to work with edit basically later on
local ZONE_NAME = "RestrictedZone" -- the name of the zone that the tools are restricted to
local MESSAGE = "This tool can only be used in the restricted zone."
local tools = game.ReplicatedStorage.tools:GetChildren() -- the tools to give to the owner of the part
local BUILD_DISTANCE = 10 -- Keep track of which players have touched the part
local touchedPlayer = nil
-- Function to check if a player owns the part they touched
local function playerOwnsPart(player, part)
return part:IsDescendantOf(player.Character) or part.Parent == player.Character
end
-- Function to give tools to the owner of the part
local function giveTools(player)
if not player.Character:FindFirstChild(tools[1].Name) then
for _, tool in ipairs(tools) do
tool:Clone().Parent = player.Backpack
end
end
end
-- Function to check if a player is within the build distance of the part they touched
local function positionWithinBuildDistance(player, part)
local character = player.Character
if not character then
return false
end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if not humanoidRootPart then
return false
end
local distance = (humanoidRootPart.Position - part.Position).Magnitude
local partRegion = Region3.new(part.Position - part.Size / 2, part.Position + part.Size / 2)
local buildDistance = partRegion.Size.magnitude / 2 + BUILD_DISTANCE
return distance <= buildDistance
end
-- Function to remove tools from the player's backpack and unequipped tool
local function removeTools(player)
-- Remove equipped tool
local equippedTool = player.Character:FindFirstChildOfClass("Tool")
if equippedTool and equippedTool:IsDescendantOf(game.ReplicatedStorage.tools) then
equippedTool.Parent = nil
return
end
-- Remove all tools from backpack
for _, tool in ipairs(player.Backpack:GetChildren()) do
if tool:IsA("Tool") and tool:IsDescendantOf(game.ReplicatedStorage.tools) then
tool.Parent = nil
end
end
-- Remove HopperBins
for _, hopperBin in ipairs(player.Character:GetChildren()) do
if hopperBin:IsA("HopperBin") and hopperBin:IsDescendantOf(game.ReplicatedStorage.tools) then
hopperBin:Destroy()
end
end
end
-- Function to check if a player is in the restricted zone and remove their tools if they're not the owner
local function checkPlayer(player)
local character = player.Character
if not character then
return
end
local part = game.Workspace:FindFirstChild(ZONE_NAME)
if not part then
return
end
if playerOwnsPart(player, part) then
if not touchedPlayer or touchedPlayer == player then
touchedPlayer = player
giveTools(player)
end
else
if positionWithinBuildDistance(player, part) then
if touchedPlayer and touchedPlayer == player then
touchedPlayer = nil
removeTools(player)
end
else
if player.Character:FindFirstChild(tools[1].Name) then
player:Kick(MESSAGE)
removeTools(player) -- remove tools if kicked
end
end
end
end
-- Check all players every frame
game:GetService("RunService").Heartbeat:Connect(function()
for _, player in ipairs(game.Players:GetPlayers()) do
checkPlayer(player)
end
end)
i’m not asking for a rewrite or anything, i just need help lol