Region3 Tool Giver?

Greetings, I’ve tried to search online for a script that gives tool, not when player joins the game, but when stepped on a part, and remove when they off the part. There wasn’t any solution for my question online, so I went and asked some friends, they suggested me using “Region3” although I am new to scripting I am trying my best to learn it. Can someone send me a script which gives a tool when stepped on part and remove when they leave it?

Thanks! <3

This isn’t using region 3, but its another way.

local Gp = game.Workspace.ToolGiver -- change thi s to the part you want to give a tool
local Tool = game.ReplicatedStorage.Tool --Change this to where your tool is located.

Gp.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter( hit.Parent) then

local New_Tool = Tool:Clone()
New_Tool.Parent = hit.Parent

end
end)

Gp.TouchEnded:Connect(function(hit)
if game.Players:GetPlayerFromCharacter( hit.Parent) then
if hit.Parent.Tool then
hit.Parent.Tool:Destroy()
else
game.Players:GetPlayerFromCharacter(hit.Parent).BackPack.Tool:Destroy()
end
end
end)

You could’ve at least attempted to make the script, by searching for general Region3 questions.

Anyways, Region3.new is used to create a new Region, and it has two parameters / arguments, which are min and max. The way Region3’s are made is that they form a box from two corners: min (being the top-left), and max (being the bottom-right). So we can write a function to create a function from a given position and size:

function createRegion3(pos, size)
    return Region3.new(pos - (size / 2), pos + (size / 2))
end

So, we have a region created, we now want to constantly register new players in the Region3 and give them the tool, and deregister leaving players and remove the tool. We can do this with a while loop:

-- plate is just the part you want the players to stand on to receive the tool. The Region3's size is increased by 10 studs to allow players to registered
local region = createRegion(plate.Position, plate.Size + Vector3.new(0, 10, 0))
local list = {} -- holds players who are currently in the region

while true do
    local newlist = {} -- used for reiterating through the players in the region3 and updating the list
    -- Arguments for FindPartsInRegion3: region, ignoreDescendantsModel, maxParts
    -- we want to ignore the plate itself in the table, so plate is the 2nd argument, and we want to get as many players in the region3, so maxParts is set to math.huge (the biggest number that roblox can get)
    local parts = workspace:FindPartsInRegion3(region, plate, math.huge)
    for _, v in pairs(parts) do
        local player = game.Players:GetPlayerFromCharacter(v.Parent)
        if player then -- verifying if it is a player who is in the region
            if not table.find(list, player) then -- checking if the player is not in the table
                list[#list + 1] = player -- adding player to the main list
                newlist[#newlist + 1] = player -- also adding player to the new list
                tool:Clone().Parent = player.Backpack -- giving the player the tool (tool is the tool you want the player to have)
            end
        end
    end
    
    for _,v in pairs(game.Players:GetPlayers()) do
        if not table.find(newlist, v) then -- checking if the current player is not in the newlist
            local index = table.find(list, player) -- finding if the player is in the table, if found, retrieve the index of the entry they're in
            if index then -- we found the index
                table.remove(list, index) -- removing the player from the list
                if player.Character and player.Character:FindFirstChild(tool.Name) then -- if the player's character exists, we'll try to find the tool in the player's character. If found, it means that the player has equipped the tool
                    player.Character[tool.Name]:Destroy()
               elseif player.Backpack:FindFirstChild(tool.Name) then
                     player.Backpack[tool.Name]:Destroy()
                end
            end
        end
    end
    wait() -- yielding the script of course, we don't to cause the script to time-out
end

Resources:

3 Likes