Hey Roblox developers! I have a system that detects when a player enters a zone, and when the player enters the zone it shows gui according to how many players are in the zone and in the game. The thing is, it detects the players in the zone through the character model in the workspace, which detects the player as its parts (leftfoot, rightfoot, left ankle, etc). When the gui shows, it says the number 16 (since it’s counting the parts of the player and not just the player). I’ve tried referring the player as “playerInZone.Parent” but it gives me errors.
here is my script:
local helicopterZone = game.Workspace.helicopterZone
local numberOfPlayersInZone = game.ReplicatedStorage.numberOfPlayersInZone
local playersInZone = {}
script.Parent.Touched:Connect(function(playerInZone)
if game.Players:GetPlayerFromCharacter(playerInZone) then
if playerInZone.Parent:findFirstChild("Humanoid") then
local char = playerInZone.Parent
local player = game.Players:FindFirstChild(char.Name)
table.insert(playersInZone, 1, "Player")
for i, player in pairs (playersInZone) do
numberOfPlayersInZone.Value = #playersInZone
end
game.ReplicatedStorage.ChangePlayerGui:FireClient(player)
end
end
end)
script.Parent.TouchEnded:Connect(function(playerThatLeftZone)
if playerThatLeftZone.Parent:findFirstChild("Humanoid") then
table.remove(playersInZone, 1)
numberOfPlayersInZone.Value = numberOfPlayersInZone.Value - 1
end
end)
please let me know if theres any way to make it detect the player as one, and not just all of its parts.
local helicopterZone = game.Workspace.helicopterZone
local numberOfPlayersInZone = game.ReplicatedStorage.numberOfPlayersInZone
local playersInZone = {}
script.Parent.Touched:Connect(function(partHit)
if partHit.Parent:FindFirstChildOfClass("Humanoid") then
local char = partHit.Parent
local player = game:GetService("Players"):GetPlayerFromCharacter(char)
table.insert(playersInZone, 1, "Player")
for i, player in pairs (playersInZone) do
numberOfPlayersInZone.Value = #playersInZone
end
game.ReplicatedStorage.ChangePlayerGui:FireClient(player)
end
end)
script.Parent.TouchEnded:Connect(function(partLeft)
if partLeft.Parent:FindFirstChildOfClass("Humanoid") then
table.remove(playersInZone, 1)
numberOfPlayersInZone.Value = numberOfPlayersInZone.Value - 1
end
end)
the same thing happened. Heres a picture of what it looks like
it shows 16 because its reading the player parts. I know because I printed it out. I also tried putting it to playersInZone.Parent but it says that “playerGui” is not a part of workspace.Gvvgghnu684 (my character). It probably looks a bit weird because I’m still testing my game.
I recommend you use Region3 | Roblox Creator Documentation for this type of thing.
I think you missed some places where it needed .Parent and checks to make sure the player is already inside of the zone. Pcalls are also recommended so they can catch errors and make it easier for you incase of an error
local helicopterZone = game.Workspace.helicopterZone
local numberOfPlayersInZone = game.ReplicatedStorage.numberOfPlayersInZone
local playersInZone = {}
script.Parent.Touched:Connect(function(playerInZone)
local successful, failMessage = pcall(function()
if game.Players:GetPlayerFromCharacter(playerInZone.Parent) then
if playerInZone.Parent:findFirstChild("Humanoid") then
local char = playerInZone.Parent
local player = game.Players:GetPlayerFromCharacter(playerInZone.Parent)
if not table.find(playersInZone, player) then
table.insert(playersInZone, player)
for i, player in pairs (playersInZone) do
numberOfPlayersInZone.Value = #playersInZone
end
game.ReplicatedStorage.ChangePlayerGui:FireClient(player)
end
end
end
end)
if successful then
if game.Players:GetPlayerFromCharacter(playerInZone.Parent) then
if playerInZone.Parent:findFirstChild("Humanoid") then
local char = playerInZone.Parent
local player = game.Players:GetPlayerFromCharacter(playerInZone.Parent)
if not table.find(playersInZone, player) then
table.insert(playersInZone, player)
for i, player in pairs (playersInZone) do
numberOfPlayersInZone.Value = #playersInZone
end
game.ReplicatedStorage.ChangePlayerGui:FireClient(player)
end
end
end
else
warn(failMessage)
end
end)
script.Parent.TouchEnded:Connect(function(playerThatLeftZone)
local successful, failMessage = pcall(function()
if playerThatLeftZone.Parent:findFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(playerThatLeftZone.Parent)
if table.find(playersInZone, player) then
table.remove(playersInZone, table.find(playersInZone, player))
numberOfPlayersInZone.Value = numberOfPlayersInZone.Value - 1
end
end
end)
if successful then
if playerThatLeftZone.Parent:findFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(playerThatLeftZone.Parent)
if table.find(playersInZone, player) then
table.remove(playersInZone, table.find(playersInZone, player))
numberOfPlayersInZone.Value = numberOfPlayersInZone.Value - 1
end
end
else
warn(failMessage)
end
end)
I’m not sure how you wwould fix this, but I do see why “playerInZone.Parent” gives you errors
playerInZone is a list (array i think its called)
You want the parent of the ITEMS in playerInZone, not playerInZone itself
It’s like eating a plate instead of the fruits inside the plates
in your playersInZone table, instead of adding the string “Player” add the name of the player. then in here
you could just add another if statement which should be something like this. this would check if a part of the player is already in the zone and if it is it just ignores it.
if not table.find(playersInZone,playerInZone.Parent.Name) then
... -- make sure to add a end!
The code didn’t work. Maybe it’s because I don’t really know what to put inside your if statement. I already found a solution to my problem, I’m just trying to explore new things since I’m new to lua and roblox developing.
edit: nvm, I see what your code does and how it works. I appreciate it for helping