In my game i have these “Training areas” that when the player enters them give the player a boost but it uses the .Touched function that fires a lot of events at the same time. example: 15:59:13.317 Touched (x32)
script.Parent.Touched:Connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
if player.leaderstats.Power.Value >= 1000000000000 and player.Character.Humanoid.Health > 0 then
player.multi.Value = 50
print("Touched")
end
end
end)
script.Parent.TouchEnded:Connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
player.multi.Value = 1
print("unTouched")
end
end)
You should make a table for every player that entered the area and remove them when they leave the area, obviously make checks using the table to make sure the same player doesn’t get added 2+ times
As mentioned by Cenn, try to use tables to store values.
Here’s an example of a system you could use. I haven’t tested it, but should work.
local Players = game:GetService("Players")
local universePlayers = {}
local universePlates = {} -- Store your plates here or manually call them in the scritps below.
local function steppedOn(user, pad)
if user.Parent:FindFirstChild("Humanoid") then
local character = user.Parent
local player = Players:GetPlayerFromCharacter(character)
if not table.find(universePlayers, player) then
table.insert(universePlayers, player)
else
return warn("Player already in table")
end
yourBoostFunction()
end
end
local function steppedOff(user, pad)
if user.Parent:FindFirstChild("Humanoid") then
local character = user.Parent
local player = Players:GetPlayerFromCharacter(character)
if table.find(universePlayers, player) then
local removeIndex = table.find(universePlayers, player)
table.remove(universePlayers, removeIndex)
else
return warn("Player not found in table...?")
end
end
end
for i, v in pairs(universePlates) do
if v:IsA("Model") then
v.HitBox.Touched:Connect(function(p)
steppedOn(p,v)
end)
v.HitBox.TouchEnded:Connect(function(p)
steppedOff(p,v)
end)
end
end