What do you want to achieve?
I want to know when a player is in a part .
What is the issue?
I can’t know when a player is in a part …
What solutions have you tried so far?
I’ve tried this but it doesn’t work :
workspace.Plot1BaseplateCollision.Touched:Connect(function()
print("plauer is inside")
end)
workspace.Plot1BaseplateCollision.TouchEnded:Connect(function()
print("plauer is outside")
end)
Hi, so there are two ways I know how to get if a player is in a part, Region3 and BasePart:GetTouchingParts. Lets try the second one. What you could do is Plot1BaseplateCollision:GetTouchingParts and put that in a for loop. If a part or player is inside or touching the part it will be in this table. Then check for the player so try
while wait(0.1) do -- check every 0.1 seconds
for i, v in pairs(Plot1BaseplateCollision:GetTouchingParts) do
local humanoid = v.Parent:FindFirstChild("Humanoid") -- looks for humanoid
if humanoid then -- checks if it finds the humanoid
print("player is inside")
end
end
end
while wait(0.1) do
for i, v in pairs(workspace.Plot1BaseplateCollision:GetTouchingParts()) do
local Humanoid = v.Parent:FindFirstChild("Humanoid")
if Humanoid then
print("player is inside")
end
end
end
It’s the first time I use for loops like this, I’m a quite lost …
Do any errors print? Also, I recommend putting that inside task.spawn(function() OR spawn(function() so it doesn’t yield any other code UNLESS it’s the only thing running in the script OR you want it to yield
Yield means it won’t run anything else past the loop while the loop is running
The best implementation is probably using the spatial query API which works especially well with finding parts within other parts. You can use this with the player’s character’s part:
local players = game:GetService('Players')
local playersInPartLastIteration = {} -- players who were in the part in the last iteration
local partToCheckCollision = script.Parent
while true do
local parts = workspace:GetPartsInPart(partToCheckCollision)
local possiblePlayers = {} -- players who are in the part
for _, part in parts do
local player = players:GetPlayerFromCharacter(part.Parent)
if player and not table.find(possiblePlayers, player) then -- we check that their player hasn't been found in the part already, append to the table of possible players
table.insert(possiblePlayers, player)
end
end
for _, player in possiblePlayers do
if not table.find(playersInPartLastIteration, player) then
-- the player wasn't found in the last iteration so this is their first time in the part
print('Player entered part')
end
end
for _, player in playersInPartLastIteration do
if not table.find(possiblePlayers, player) then
-- player was found in last iteration but not this iteration so this is their first time leaving the part
print('Player left part')
end
end
task.wait(0.1) -- you don't really need to check every frame, 0.1 seconds should suffice
-- finally, set the last iteration to be the result from this iteration
playersInPartLastIteration = possiblePlayers
end
this Module Can Check If Player Entered Part Or Exited From It and Can Check Any Item You Can Add it To Check if Entered or Not
Example:
local container = workspace.AModelOfPartsRepresentingTheZone
local zone = Zone.new(container)
zone.playerEntered:Connect(function(player)
print(("%s entered the zone!"):format(player.Name))
end)
zone.playerExited:Connect(function(player)
print(("%s exited the zone!"):format(player.Name))
end)
local PlayersInside = {}
Plot1BaseplateCollision.Touched:Connect(function(hit)
if hit and hit.Parent:FindFirstChildWhichIsA("Humanoid") then
table.insert(PlayersInside, hit.Parent.Name)
print(PlayersInside[hit.Parent.Name].." just entered in the zone!")
end
end)
Plot1BaseplateCollision.TouchEnded:Connect(function(hit)
if hit and hit.Parent:FindFirstChildWhichIsA("Humanoid") then
table.remove(PlayersInside, hit.Parent.Name)
print("A player just left the zone!")
end
end)
So when I went in the part that prints all the time :
Player has entered
Player has leave
Player has entered
Player has leave
…
So I want know by who way I can Know if a player is inside a part !
Plot1BaseplateCollision.Touched:Connect(function(hit)
if hit and hit.Parent:FindFirstChildWhichIsA("Humanoid") then
print(hit.Parent.Name.." just entered in the zone!")
end
end)
Plot1BaseplateCollision.TouchEnded:Connect(function(hit)
if hit and hit.Parent:FindFirstChildWhichIsA("Humanoid") then
print(hit.Parent.Name.." just left in the zone!")
end
end)