I am making a script that adds the player to a folder if they enter a part called Zone.
Problem: Player is not going to folder when entering zone
Code:
local InfoService = require(game.ServerScriptService.Core.InfoService)
local function onTouched(part)
if part:IsA("BasePart") and part.Name == "Zone" then
local character = part
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if character:IsA("Model") and humanoidRootPart and InfoService.playerFolder then
if InfoService.playerFolder:IsA("Folder") then
character.Parent = InfoService.playerFolder
else
warn("Attempted to parent to an invalid folder.")
end
end
end
end
local function onTouchEnded(part)
local character = part
if character:IsA("Model") and InfoService.byPlayerFolder then
if InfoService.byPlayerFolder:IsA("Folder") then
character.Parent = InfoService.byPlayerFolder
else
warn("Attempted to parent to an invalid folder.")
end
end
end
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
humanoidRootPart.Touched:Connect(onTouched)
humanoidRootPart.TouchEnded:Connect(onTouchEnded)
end)