Okay, so I’ve been scripting this vent system for a game to detect when a player enters and exits a vent.
And then takes away health after being there for nine seconds.
But that’s besides the point, I want to know if there’s anything I can do the improve on in this script, and anything I can do. Thanks for anyhelp by the way!
-- Getting the player's humanoid
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
Humanoid = Character:WaitForChild("Humanoid")
HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
end)
end)
-- Getting sound and sound part for death
local SoundPart = game.Workspace.SoundPart
local Sound = SoundPart.DeathVentSound
-- Variables for checking if the part has been touched and if user has left vent
local PartIsTouched = false
local LeftVent = false
--Getting the parts for the time in workspace.
local StartVentPart = game.Workspace.StartTimePart
local EndVentPart = game.Workspace.EndTimePart
-- Getting remote event for server to client
local VentEvent = game.ReplicatedStorage.VentBorder
-- Getting the monster
local Monster = game.Workspace.Carnage
-- Function for monster
local function SpawnMonster()
Monster:MoveTo(Vector3.new(HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y, HumanoidRootPart.Position.Z + 5))
wait(2)
Monster:MoveTo(Vector3.new(-76.382, 3.425, -16.122))
end
StartVentPart.Touched:Connect(function() -- Whenever the part is touched
if PartIsTouched == false then -- If we haven't touched the part, do the code below
PartIsTouched = true
LeftVent = false
VentEvent:FireClient()
for ventTime = 0, 9, 1 do -- For loop for the time detection
if LeftVent == false and Humanoid.Health > 0 then
print(ventTime)
wait(1)
else
break
end
end
while LeftVent == false and Humanoid.Health > 0 do -- Loop if player is still in vent
wait(2)
Humanoid:TakeDamage(26)
end
SpawnMonster() -- Name is self explainitory
else -- If we have touched it, do nothing
end
end)
EndVentPart.Touched:Connect(function()
LeftVent = true
PartIsTouched = false
end)
I would expect bugs from such code if you haven’t already encountered them, assuming that there isn’t so much as a debounce (or a physical obstacle) that restricts the time between each entrance to the vent.
If such restrictions exist, then you may disregard the following note:
Suppose a player enters the vent; let’s say he has waited 11 seconds. Immediately after those eleven seconds, he leaves the vent. Before 2 another seconds, he quickly enters the vent again. Suddenly, he starts taking damage even though it hasn’t even been nine seconds since his entrance.
Why?
Right after he left the vent after the (approximately) 11 seconds, the code will be waiting for the wait(2) to finish. But before it finishes, the player enters the vent again. The player will then start taking damage because the wait(2) has just finished, and the criteria that allows the while loop to take place, will be true(even though the variables have obviously changed after the player left), but the code doesn’t know that.
How I would fix it (without debounces/hard-coded time delays):
Add a counter variable that measures the amount of times the player has entered the vent.
Increment the variable as such using variable++ in the .Touched callback.
In the same callback, preserve the value of the variable in a local variable.
As another criteria (with “and”) in the while loop, check if the local variable is equal to the outer variable (that measures the amount of times the player has entered the vent).
Wrap the “SpawnMonster()” in an if statement with that same var1 == var2 criteria.
Also, don’t forget to include the argument that should be passed into the FireClient(), which should be the player object.
Now, coding-wise:
I would compress the entire for loop inside the .Touched callback entirely into a task.wait(9)
(do so if my suggestion above has been implemented.)
If not, remove the “,1” in the “for ventTime = 0, 9, 1 do” as that is virtually included by default.
Why would I use workspace instead of game.WorkSpace()?? I know what task.wait() does, but confused on using workspace since I’m sure those two are the same.