I have a sword fighting game where the players have different weapons in their backpack, I have an area on a map that doesn’t allow weapons. when a player enters this area I want to store the players inventory in a table or something so when they leave they get their items back.
the issue would be that I dont know how to do this and need help
i cant really supply scripts because i havent tried anything yet
Once the player enters the area that doesn’t allow weapons, create a folder with the players name in ServerStorage/ReplicatedStorage, and then store their items there in the folder. Once they leave, put the items back in the players backpack.
A convenient solution here would be to use a module to contain this logic, then you can use it independently from your other systems when ever you want to load/store the player’s items in a table.
I’ll write you a basic system for this:
--//Assuming this is placed in a module
local inventoryManager = {}
--//Private definitions
local cachedTools = {}
function inventoryManager:storeTools(player)
--//Initialization if no table is cached for this player
cachedTools[player] = {}
for _, tool in player.Backpack:GetChildren() do
--//No filtering is done here, so you may need to make it so only certain tools get stored
table.insert(cachedTools[player], tool)
tool.Parent = nil
end
end
function inventoryManager:loadTools(player)
--//Restores all of the player's tools (if any)
if cachedTools[player] then
for _, tool in cachedTools[player] do
tool.Parent = player.Backpack
end
--//Refresh the table
cachedTools[player] = {}
end
As far as detecting when players enter zones go, I think raycasting is the best option in terms of efficiency. It works very well too. Make a folder or some type of container that holds different parts that will map out each zone. Position and size it like a floor, with it not being collidable by the player.
You can specify a white list where the ray will only hit those zone parts, so if it hits one, the player is within one of these zones.
Here’s some pseudocode for that type of functionality:
--//This can be a script placed as a StarterCharacterScript for example
local UPDATE_FREQUENCY = 1/20 --//The update frequency in which new zones are tested for
local RAY_DIRECTION = Vector3.new(0, -6, 0) --//The direction in which the ray is cast, this should work in most scenarios
local zones = workspace.Zones --//Where your zone instances are stored
local timer = 0
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {zones} --//We only want our raycasts to hit these parts
raycastParams.FilterType = Enum.RaycastFilterType.Include
local runService = game:GetService("RunService")
local function isInZone() --//Determines if the player is in one of the anti-weapon zones
local result = workspace:Raycast(script.Parent.PrimaryPart.Position, RAY_DIRECTION, raycastParams)
return result ~= nil --//Returns true if the player is in a zone, returns false if not. Only works for one general zone so you may need to configure this to your needs.
end
runService.Heartbeat:Connect(function(dt)
timer += dt
if timer >= UPDATE_FREQUENCY then
timer = 0
--//Determine if the player is in any zone
if isInZone() then --//Use method calls from the inventory manager module to handle loading/storing player tools
print("The player is in a zone!")
else
print("The player is not in a zone!")
end
end
end)
I whipped this up pretty quickly, so don’t expect it to work perfectly. Hopefully it gives you a decent structure to go about implementing this for yourself.