So basically, I am making a swordfight area where you get a sword while being there.
Problem is that the code that checks if a player left the area is always running, not allowing the player to have a sword at all.
local repStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local Environment = workspace:WaitForChild("Environment")
local Gameplay = workspace:WaitForChild("Gameplay")
local regionblock = Gameplay.Regions.SwordfightRegion
local ignoredInstances = Environment.MainIsland.Swordfight:GetChildren()
local modules = repStorage.modules
local sfRegion = Region3.new( (regionblock.Position - regionblock.Size / 2) , (regionblock.Position + regionblock.Size/2) )
local isInRegion = require(modules.swordfighters)
local sword = game:GetService("ServerStorage").Sword
while true do
local insideRegion = workspace:FindPartsInRegion3WithIgnoreList(sfRegion, ignoredInstances)
for i, instance in pairs(insideRegion) do
local hum = instance.Parent:FindFirstChild("Humanoid")
if hum then
local char = hum.Parent
local plr = players:GetPlayerFromCharacter(char)
if not isInRegion[plr] then
isInRegion[plr] = plr
--print(isInRegion[plr])
local newSword = sword:Clone()
sword.Parent = plr.Backpack
end
end
end
for i, plr in pairs(isInRegion) do
local char = plr.Character
--print(char)
print(insideRegion[i])
if insideRegion[plr] == nil then
local sword = char:FindFirstChild("Sword")
local sword2 = plr.Backpack:FindFirstChild("Sword")
if sword then
sword:Remove()
elseif sword2 then
sword2:Remove()
end
isInRegion[plr] = nil
end
end
wait()
end
insideRegion table always returns nil for some reason.