So i have 2 scripts in studio that are connected and i want someone to add something to my script but join me in studio because i think that i have an issue,
To this script below i want the function that kills a player to wait for 1 second, so when the “RedLight” Textlabel has been visible at least 1 second THEN after that it kills any player who moves.:
As you can tell its a red light green light game and my current script kills them too fast and makes it almost impossible to stop at red light without being too slow
local fireArea = workspace.RedLightGreenLight.HitParts:FindFirstChild("FireArea")
local fireAreaSize = fireArea.Size / 2
local redLightStatusEvent = game.ReplicatedStorage:WaitForChild("RedLightStatus")
-- Variable to track if RedLight is visible
local redLightVisible = false
-- Function to check player movement in FireArea
local function checkPlayerMovementInFireArea()
game:GetService("RunService").Heartbeat:Connect(function()
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
local char = player.Character
if char then
local hrp = char:FindFirstChild("HumanoidRootPart")
local humanoid = char:FindFirstChild("Humanoid")
if hrp and humanoid then
local min, max = fireArea.Position - fireAreaSize, fireArea.Position + fireAreaSize
if hrp.Position.X >= min.X and hrp.Position.X <= max.X and
hrp.Position.Y >= min.Y and hrp.Position.Y <= max.Y and
hrp.Position.Z >= min.Z and hrp.Position.Z <= max.Z and
humanoid.MoveDirection.Magnitude > 0 and redLightVisible then
wait(2) -- Wait for 2 seconds before killing
humanoid.Health = 0
end
end
end
end
end)
end
-- Listen for changes in RedLight status from the client
redLightStatusEvent.OnServerEvent:Connect(function(_, isVisible)
redLightVisible = isVisible
if redLightVisible then
checkPlayerMovementInFireArea()
end
end)
basically update the script so that it checks if players are moving after the “RedLight” text has been visible for at least 1 second
Though you are waiting two seconds before killing the player, it is only after they’ve been detected to be moving while the red light is active, and this is detected immediately as it is activated. Instead, you need to wait two seconds before calling checkPlayerMovementInFireArea
.
On another note, you need to make sure you disconnect the RBXScriptConnection returned to you by RBXScriptSignal:Connect. This is the basis of memory-managment with events. If you do not disconnect your event listener, you will continue to listen for players moving even after the red light is deactivated. The next time it activates, you will duplicate the code listening for movement, and this will continue to happen indefinitely. What I have just described to you is called a “memory leak”
Finally, I am concered by your RemoteEvent. There is no reason why a client should be in control of whether or not the red light is active, not unless you’re expressly giving control to a specific user. If that’s the case, you must verify it’s that user who’s firing the remote event, otherwise exploiters could trigger killing at will, and with your memory leak, cause a performance decline for the entire server
1 Like
Could you please come in to studio on my game for a minute and fix it for me? Thank you so much, if i could add you and if you joined me on studio for a minute
No. I gave you sufficient guidance. You must learn to develop these things on your own
I can’t guarantee that this will work, but it’s worth a try!
local connection = nil
local fireArea = workspace.RedLightGreenLight.HitParts:FindFirstChild("FireArea")
local fireAreaSize = fireArea.Size / 2
local redLightStatusEvent = game.ReplicatedStorage:WaitForChild("RedLightStatus")
-- Variable to track if RedLight is visible
local redLightVisible = false
-- Function to check player movement in FireArea
local function checkPlayerMovementInFireArea()
wait(2)
connection = game:GetService("RunService").Heartbeat:Connect(function()
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
local char = player.Character
if char then
local hrp = char:FindFirstChild("HumanoidRootPart")
local humanoid = char:FindFirstChild("Humanoid")
if hrp and humanoid then
local min, max = fireArea.Position - fireAreaSize, fireArea.Position + fireAreaSize
if hrp.Position.X >= min.X and hrp.Position.X <= max.X and
hrp.Position.Y >= min.Y and hrp.Position.Y <= max.Y and
hrp.Position.Z >= min.Z and hrp.Position.Z <= max.Z and
humanoid.MoveDirection.Magnitude > 0 and redLightVisible then
humanoid.Health = 0
end
end
end
end
end)
end
-- Listen for changes in RedLight status from the client
redLightStatusEvent.OnServerEvent:Connect(function(_, isVisible)
redLightVisible = isVisible
if redLightVisible then
checkPlayerMovementInFireArea()
else
if connection then
connection:Disconnect()
connection = nil
end
end
end)
1 Like
made it worse and kills me after 0.1 seconds
Hmmmmmm. Let me look into some more things.
Ok. So the problem with your original script is that the wait() is after the conditional (if/then statement) that checks if the player is in the zone. So what your script does is when it becomes red light, if the player is still moving, it waits 2 seconds, and then regardless of whether they are moving or not then, it kills them. Another problem is that the RunService.Heatbeat event keeps going after the function ends, so even if the cooldown worked, it would not work a second time.
So this is a re-edited version that again may or may not work. Hope it helps!
local fireArea = workspace.RedLightGreenLight.HitParts:FindFirstChild("FireArea")
local fireAreaSize = fireArea.Size / 2
local redLightStatusEvent = game.ReplicatedStorage:WaitForChild("RedLightStatus")
-- BoolValue Instance to track if RedLight is visible
local redLightVisible = Instance.new("BoolValue")
redLightVisible.Value = false
redLightVisible.Parent = script
--Variable that determines if script should kill players
local KillThePlayer = false
game:GetService("RunService").Heartbeat:Connect(function()
if KillThePlayer then
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
local char = player.Character
if char and then
local hrp = char:FindFirstChild("HumanoidRootPart")
local humanoid = char:FindFirstChild("Humanoid")
if hrp and humanoid then
local min, max = fireArea.Position - fireAreaSize, fireArea.Position + fireAreaSize
if hrp.Position.X >= min.X and hrp.Position.X <= max.X and
hrp.Position.Y >= min.Y and hrp.Position.Y <= max.Y and
hrp.Position.Z >= min.Z and hrp.Position.Z <= max.Z and
humanoid.MoveDirection.Magnitude > 0 then
humanoid.Health = 0
end
end
end
end
end
end)
-- Listen for changes in RedLight status from the client
redLightStatusEvent.OnServerEvent:Connect(function(_, isVisible)
redLightVisible.Value = isVisible
if redLightVisible.Value then
wait(2)
KillThePlayer = true
else
KillThePlayer = false
end
end)