Hello. I’ve got an problem.
I want to teleport a players with a tag to another location. By that the players are touching a part. When they touch the part they will get a tag. My problem is now when the player doesn’t touch the part that they still have the tag (Means they will still get teleported). I don’t know how to fix it. Can somebody please help me. Thanks. Code:
local CollectionService = game:GetService("CollectionService")
script.Parent.Parent.Aentrance.Touched:Connect(function(h)
local hum = h.Parent:FindFirstChild("Humanoid")
if hum ~= nil then
CollectionService:AddTag(hum, "IsTouching")
end
end)
script.Parent.Parent.Aentrance.TouchEnded:Connect(function(h)
if CollectionService:HasTag(h, "IsTouching") then
CollectionService:RemoveTag(h, "IsTouching")
end
end)
script.Parent.ClickDetector.MouseClick:connect(function()
for i, v in pairs(CollectionService:GetTagged("IsTouching")) do
local hum = v
hum.Parent.HumanoidRootPart.CFrame = CFrame.new(script.Parent.Parent.Aexit.Position)
end
wait (3)
end)
You’re assigning the IsTouching tag to the characters humanoid when the .Touched event is fired, however when the .TouchEnded event is fired you’re performing the logic on the instance which fired the event instead of the humanoid. The function should look more along the lines of:
script.Parent.Parent.Aentrance.TouchEnded:Connect(function(h)
local hum = h.Parent:FindFirstChild("Humanoid")
if hum ~= nil then
if CollectionService:HasTag(hum, "IsTouching") then
CollectionService:RemoveTag(hum, "IsTouching")
end
end
end)
You might also want to validate that h is the HumanoidRootPart or equivalent, because as is if the character sticks a limb out it will trigger the event and perform the tag removal logic on them despite not exiting the area for the most part.
Since you are adding the tag to the humanoid, the touchended does not remove the tag from the humanoid and instead tries to remove it from the part that touched?
By ensuring the HumanoidRootPart (the characters root part) is the part which triggered the event, it won’t be triggered if the character sticks a limb out if they’re sitting too close to the edge of the barrier.
script.Parent.Parent.Aentrance.TouchEnded:Connect(function(h)
local hum = h.Parent:FindFirstChild("Humanoid")
if hum ~= nil and h.Name == "HumanoidRootPart" then
if CollectionService:HasTag(hum, "IsTouching") then
CollectionService:RemoveTag(hum, "IsTouching")
end
end
end)
This was just a suggestion by me if you wanted extra logic like this. Whether you implement it or not depends on your setup and desired outcome.
ahh ok. Now I understand. But somehow now the teleportation doesn’t work anymore. I have looked that the player stands completly in the area but it doesn’t work.
The green shaded area is where the humanoidrootpart is located. If you applied the same logic to both functions and the green part doesn’t touch the brick, then it will not assign or remove the tag, therefore they will not be teleported. If your brick, for example, only goes up to the characters legs then it won’t touch the root part, which is why I said to implement the change depending on your setup and desired outcome. Other than that, it should work perfectly fine because the only change we implemented was fixing the CollectionService tag being removed.