So I want to a make house arrest thing, and I got this little script, it wont print anything, and im curious if I did something wrong?
local Players = {} -- players in region gets stored into this table
local AreaPart = workspace.AS
function createRegion3FromPart(part)
local Size = part.Size
local Position = part.Position
return Region3.new(Position-(Size/2), Position+(Size/2))
end
function getPlayersInPart(part)
for k,v in pairs(Players) do
if part and Players.v.Parent.Name == script.Parent.Arrested.Value then
print("Found owner")
local newRegion = createRegion3FromPart(part)
local partsInRegion = workspace:FindPartsInRegion3(newRegion, nil, math.huge)
local tempList = {} --this list resets each time
for i, v in pairs(partsInRegion) do
if game.Players:FindFirstChild(v.Parent.Name) then
local player = game.Players[v.Parent.Name]
table.insert(tempList, player)
if not Players[player] then
table.insert(Players, player)
end
end
end
for k,v in pairs(tempList) do --loop over every player found in this single check
if not Players[v] then --if player hasn't been found which was found earlier,
table.remove(Players, v) --remove the player.
print("player gone")
end
end
end
end
end
while true do
for i, v in pairs(Players) do
print(tostring(v))
end
getPlayersInPart(AreaPart) wait(1);
end
I am quite confused, of what you are trying to achieve here. As at the start of the function you are trying to iterate a table which will have no values.
local Players = {}
for k,v in pairs(Players) do
If you are trying to iterate all the players in the game at the current moment, you would want to do.
for k,v in pairs(game:GetService("Players"):GetPlayers()) do
Also on this line:
if part and Players.v.Parent.Name == script.Parent.Arrested.Value then
If you are comparing the players name with a value which is stored in the arrested value, you should be doing this.
if part and v.Name == script.Parent.Arrested.Value then
If you were trying to detect if the arrested player was in a region, I have fixed the code you provided here:
local Players = game:GetService("Players");
local PlayersInRegion = {} -- players in region gets stored into this table
local AreaPart = workspace.AS
function createRegion3FromPart(part)
local Size = part.Size
local Position = part.Position
return Region3.new(Position-(Size/2), Position+(Size/2))
end
function getPlayersInPart(part)
for k,v in pairs(Players:GetPlayers()) do
if part and v.Name == script.Parent.Arrested.Value then
print("Found owner")
local newRegion = createRegion3FromPart(part)
local partsInRegion = workspace:FindPartsInRegion3(newRegion, nil, math.huge)
local foundPlayer = false;
for i, v in pairs(partsInRegion) do
local Player = Players:FindFirstChild(v.Parent.Name);
if Player then
foundPlayer = true;
break;
end
end
if(foundPlayer and not PlayersInRegion[v]) then
PlayersInRegion[v] = true;
elseif(not foundPlayer) then
PlayersInRegion[v] = false;
end
end
end
end
while true do
for i, v in pairs(PlayersInRegion) do
print(tostring(v))
end
getPlayersInPart(AreaPart) wait(1);
end
Hopefully, I interpreted what you wanted to achieve if this was not what you wanted just reply and I will fix it.