I am trying to make a safezone for the players. I want to either disable the usage of tools (can’t Activate the tool) OR simply make the tool disappear from the player’s backpack when inside the safezone (it will be a invisible cylinder part)
To detect if I player is inside the zone, I will use OverlapParams I think (is there a better way?)
What would be the best way to do that? Is there some propreties or functions that can help me?
To disable the usage of a tool in Roblox, you can use the CanActivate() function of the Tool class. This function allows you to control whether the tool can be activated by the player.
Here’s an example of how you can use CanActivate() to disable a tool when the player is inside a safe zone:
local tool = script.Parent
function onActivated()
-- Check if the player is inside the safe zone
local player = game.Players:GetPlayerFromCharacter(tool.Parent)
local safeZone = workspace.SafeZone
if player:IsDescendantOf(safeZone) then
-- If the player is inside the safe zone, prevent the tool from being activated
return false
else
-- If the player is not inside the safe zone, allow the tool to be activated
return true
end
end
tool.Activated:Connect(onActivated)
To make the tool disappear from the player’s backpack when inside the safe zone, you can use the Destroy() function of the Instance class to delete the tool from the game. Here’s an example of how you can do this:
local tool = script.Parent
function onActivated()
-- Check if the player is inside the safe zone
local player = game.Players:GetPlayerFromCharacter(tool.Parent)
local safeZone = workspace.SafeZone
if player:IsDescendantOf(safeZone) then
-- If the player is inside the safe zone, delete the tool
tool:Destroy()
end
end
tool.Activated:Connect(onActivated)
Is there a way to remove the tool from the players backpack without Destroy()? Cuz if not, how can I make it reappear in the player’s backpack when they leave the safezone?
When you say disable, you mean that the player can still see it in his inventory but cannot use it? If thats it, then id rather just remove it and make it reappear when he leaves the safezone
local tool = script.Parent
local player = game.Players:GetPlayerFromCharacter(tool.Parent)
local safeZone = workspace.SafeZone
local hasTool = true
function onActivated()
if player:IsDescendantOf(safeZone) then
-- If the player is inside the safe zone and has the tool, delete the tool
if hasTool then
-- Move the script to the player's character
script.Parent = player.Character
-- Delete the tool
tool:Destroy()
hasTool = false
end
else
-- If the player is not inside the safe zone and doesn't have the tool, give it back to them
if not hasTool then
-- Create a new copy of the tool
local newTool = tool:Clone()
-- Give the new tool to the player
player:GiveTool(newTool)
-- Move the script to the new tool
script.Parent = newTool
hasTool = true
end
end
end
tool.Activated:Connect(onActivated)
This way, the script will continue to run even if the tool is deleted, and it will be able to give the player a new copy of the tool when they leave the safe zone.
This wouldn’t work as you don’t parent the Character to a part. The player’s character is a descendant of workspace.
Try doing a region check with the safezone part and then every half a second check who’s in the region.
Here’s a good video to understand Region checking.
In addition, to remove their tools, and keep them available for the user assuming this is a Server script do this. If it is a LocalScript just parent the tools to ReplicatedStorage. However, I would not trust the client, and would do this on the server.
local playerTools: {Tool} = {}
for _, v: Tool in ipairs(player.Backpack:GetChildren()) do
v.Parent = ServerStorage
table.insert(playerTools, v)
end
-- giving them back
for _, tool: Tool in ipairs(playerTools) do
v.Parent = player.Backpack
end
table.clear(playerTools)
I don’t think that will work lol, ChatGPT isn’t very reliable. player:IsDescendantOf checks if the Player is parented to the safe zone (or a descendant of it) and won’t check if they’re touching the safe zone.
I did this in a post a while ago by using this GetInPart function
function GetInPart(HumanoidRootPart, Part)
local check = HumanoidRootPart.Touched:Connect(function() end) -- This is just to make sure it detects non-cancollide parts
local parts = HumanoidRootPart:GetTouchingParts()
check:Disconnect()
for _, part in parts do
if part == Part then
return true
end
end
return false
end
then replacing the tools attack/activate function to return if the Player is in a safe zone.
function Blow(Hit)
if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
return
end
local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
if not RightArm then
return
end
local RightGrip = RightArm:FindFirstChild("RightGrip")
if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
return
end
local character = Hit.Parent
if character == Character then
return
end
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid or humanoid.Health == 0 then
return
end
local player = Players:GetPlayerFromCharacter(character)
if player and (player == Player or IsTeamMate(Player, player)) then
return
end
-- Code I added
local InSafeZone = GetInPart(character:FindFirstChild("HumanoidRootPart"), SafeZone)
if InSafeZone then
return
end
--
UntagHumanoid(humanoid)
TagHumanoid(humanoid, Player)
humanoid:TakeDamage(Damage)
end
Overall, I think it depends on what tool you are using as they all use different methods to handle attacks, but I did this by finding the attack function then adding a condition to check if the player is in the safe zone (along with all the other conditions) then returning if so. Also, I made this for the ClassicSword, so you’d have to find your own tools attack function, then do something like this.
I think it might take a little bit more code to remove the tool from the Players inventory if they’re in the safe zone then add it back after they leave, but if you really want to that, you could try by parenting the tool somewhere else (e.g. temp folder in ReplicatedStorage) then parenting it back to the Players inventory after they leave.
It disables the backpack (the player cant see their backpack nor use the tools inside)
after 5s, the backpack reappears and the player can use the tool. Its on a local script, so idk if theres a way to easily bypass it. I can make a remote event that launches from the server to the client when the player enters or leaves the safezone, but I still dont know if theres a way to bypass the script.