local part = script.Parent
local players = game:GetService(‘Players’)
game.ReplicatedStorage.GasDamage.OnServerEvent:Connect(function()
for i,v in pairs(players:GetPlayers()) do
–if workspace.GasEnabled.Value == true then
while wait(.1) do
local character = v.Character or v.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild(‘HumanoidRootPart’)
if (part.Position - humanoidRootPart.Position).magnitude < 50 then
v.Character.Humanoid:TakeDamage(5)
end
--end
-- else
-- break
end
end
end)
This code works but is not correct. Whenever 2 player enter a certain area, it deals damage to 1 player. I want 2 player to be damaged.
that’s because you created a pairs loop for each player and you made a while wait loop in the middle
which will make the first iteration of the player loop to run and it will make the while wait do and never stop
if you want it to do the while loop but not to stop the entire script you should use a coroutine
the code will look like this
local part = script.Parent
local players = game:GetService("Players")
game.ReplicatedStorage.GasDamage.OnServerEvent:Connect(function()
for i,v in pairs(players:GetPlayers()) do
if workspace.GasEnabled.Value == true then
coroutine.wrap(function()
while wait(.1) do
local character = v.Character or v.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
if (part.Position - humanoidRootPart.Position).magnitude < 50 then
v.Character.Humanoid:TakeDamage(5)
end
end
end)
end
end
end