Guys, please, help me to create a zone, where player gets a weapon and loses it after leaving the zone.
For a spherical zone, just check the magnitude of the player’s character’s root part and the center of your zone. If the magnitude is within the radius, then give them the tool if they don’t have it. It the magnitude is outside that radius, take away the tool.
For a rectangular prism zone, check the position is between the farthest top left corner and closest bottom right corner.
Hi. You can use a part as baseplate and give or remove the weapon using a Touched event. You can also use REALTimothy0812’s suggestion by putting a script in StarterCharacterScripts containing a loop.
The first idea seems more useful for this case.
I tried to do it with Touched event. I made a script that if you touch a part, the script checks your backpack and if there is no tool, it gives you it. But it didn’t work and I didn’t understand script’s problem.
You could use Region3 and FindPartsInRegion3 (Also FindPartsInRegion3WithIgnoreList if you need to ignore parts).
Continously loop and check if it finds a part in the region3, if it does use GetPlayerFromCharacter on the part’s Parent.
If this does return a player then check that the Tool is inside their Backpack using FindFirstChild, if not add one and add them to an array.
In the next loop, loop through that array and check that the player is still inside the Region3, if not remove the tool from their Backpack.
You can use this module to assist you with positioning the Region3 in the correct way:
I’m not very good at Lua, so I’m gonna better use the most easiest way: making a part on all the zone and uncheck CanCollide on it. Touch event works, but I don’t know how to even script a sword giving from the ServerStorage.
i will prefere FindPartsInRegion3WithWhiteList and put there all player character root parts(
for _,p in pairs(game.Players:GetChildren()) do
--code, that add p.Character.HumanoidRootPart
end
)
Well if you use GetPlayerFromCharacter on the hit.Parent then you’ll get the player, then you can add to it to their Backpack like so:
if not player.Backpack:FindFirstChild("Sword") then --check it doesn't already exist
local sword = game:GetService("ServerStorage").Sword:Clone() --clone it
sword.Parent = player.Backpack --parent it
end
I don’t see why touch events wouldn’t work. Simply check touch events locally and then send remoteevents to the server to give the player the item.
I’ve successfully made a script with giving a tool. But now, the problem is that I don’t know how to make a script with losing the tool.
Ok so, you’re going to need to continously do magnitude checks upon the player to check that they’re still in the area. Your code in total should be like:
- Create a table variable
- Create a debounce variable
- Touched event
- GetPlayerFromCharacter
- Check if it did get a player
- Debounce check
- Check the player doesn’t own the tool
- Check the player is not in this table (
if not table[player.Name] then
) - Debounce goes false
- Give the tool
- Add the player to this table (
table[player.Name] = player
) - Wait and then set debounce true
Now in a block of code underneath:
- In a while loop
- For loop through the table (
for name, player in pairs(table) do
) - Check that
player
is not nil (You’ll see why)! - Check that the player’s character exists (
if player.Character then
) [else
remove the tool from their Backapck] - Compare the magnitude between their HumanoidRootPart and this area, if it’s higher than how big the area is remove them from the table like this:
table[name] = nil
and remove the tool from their Backpack.
This is a lot to do, good luck.