Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to detect if an "if" statement has run several times in a short period of time?

Asked by 6 years ago
-- le regular script inside StarterCharacterScripts
while true do
    if trainSpawn.Enabled == true and humroot.Position.Y < 8 and hum.Health > 0 then -- if the train is active, the player is below the general stand height of the train, and the player isn't dead
        humroot.Velocity = Vector3.new(0, 0, -500) -- set the player's velocity to match the train's velocity
        humroot.CFrame = CFrame.new(trainSpawn.CFrame.X, trainSpawn.CFrame.Y + 1.35, trainSpawn.CFrame.Z) -- set the CFrame of the HumanoidRoot part to the CFrame of the invisible spawn on the train + 1.35 Y
    end
    wait()
end

-- be4 da loop

local player = script.Parent
local playerObject = game:GetService("Players"):WaitForChild(tostring(script.Parent.Name))
local humroot = player:WaitForChild("HumanoidRootPart")
local hum = player:WaitForChild("Humanoid")
local trainSpawn = game:GetService("Workspace").Train.TrainSpawn

So I haz this game

Le train into the glitchlands

where well

self-explanatory; u hop on a train and fly to the glitchlands

The script above is meant to save the player if they fall.

However, it eventually comes to constantly teleporting the player directly behind the train over and over again.

I want to make it so that if the if statement is triggered multiple times (e.g. 5 times) in a short period of time (e.g. 3 seconds), such is detected, and the CFrame.Z changes slowly each over-trigger (e.g. minus 10 per each over-teleport), and then after the player is correctly placed on the train the CFrame.Z in the script resets back.

So ye, what prescription do you suggest?

If my question does not meet a specific community guideline or in general you are going to downvote it, please comment why so that I can fix the issue before you downvote.
0
The script worked fine when I tested it. TiredMelon 405 — 6y
0
Huhr? You played the game? It doesn't work fine thou u just gotta maybe be a 'lil patient sweetkid01 176 — 6y
0
What i usually do is create a variable called count which keeps track on how many times a code has run and increases the value each time by one. The same process could be applied here. Zafirua 1348 — 6y
0
Err... But I need to keep track of the RATE at which it fires at. not just keep track of how many times it's run sweetkid01 176 — 6y
0
You can use another variable that keeps track of time, and compare the amount of time between each time the statement runs. You can get the current time to several decimal places used "tick()" dyler3 1510 — 6y

Answer this question