The block of code should make the player’s character parent to ReadyPlayers if they are in the zone.
Problem: If the player equips a tool in the zone or enters the zone with a tool equipped, then the game lags (goes from 60fps to 30fps) and constantly moves the player back and forth from ReadyPlayer to SubPlayer.
I know I could use a zone plugin, but I want to keep it simple and easy to edit in the future.
Code:
local Zone = script.Parent
local ReadyPlayers = game.Workspace.Core.Players.Ready
local PlayingPlayers = game.Workspace.Core.Players.Playing
local SubPlayers = game.Workspace.Core.Players.Sub
Zone.Touched:Connect(function(Hit)
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
if Humanoid and Character.Parent ~= PlayingPlayers then
Character.Parent = ReadyPlayers
end
end
end)
Zone.TouchEnded:Connect(function(Hit)
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
local Character = Player.Character
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
if HumanoidRootPart and Character.Parent ~= PlayingPlayers then
Character.Parent = SubPlayers
end
end
end)
You could add a debounce on the touched function and you should probably make a table which you insert the player’s into when they are within the zone’s radius and then check the table whenever something hits the zone.
Thanks for that! It did not work, but I found the problem. The issue is that when the player has a tool equipped inside the zone, it causes the Touch event and EndTouch event to fire, causing the back and forth.
Debounce: This is a good idea, but because of the issue, it will just come true and false back and forth.
I don’t know how to fix this.
I could cheat and just use two parts, having one do one and the other do the other. but I want to finger this out.
i stumbled across this post by @RuizuKun_Dev about using .Touched and .TouchEnded properly. it includes a short video in it (see “normal”) that showcases a problem similar to yours. the character equips the tool and the .Touched and .TouchEnded events fire simultaneously.
try modifying some of their code from the tutorial (in paticular, the MyImplementation() function") and see what works for you.