What Is Time Scale Framework?
Time Scale Framework is something I wrote to allow time to slow down or speed up mid-game.
Learn More
Setup
- Grab the model link and insert it into studio.
- Move the folder to ServerScriptService.
Whitelisting Objects
This framework works by whitelisting objects. This means that only objects that are tagged with TimeScaleWhitelist will be affected by time scale changes. This is to help with performance as affecting every part in the game (even static ones) wouldn’t be the best for performance so only tag instances that need to be slowed.
The tags in the game use CollectionService. A great way to set these in studio in to use this plugin. Read more about the plugin here!
To whitelist an object so that the framework knows it needs to be affected by time scale changes, tag it with specifically a tag named “TimeScaleWhitelist”.
Now that the instances you want whitelisted are set, we can now move on to making it so the player can control time for this demonstration.
Scripts
Insert a Script into ServerScriptService. This script will handle setting up a RemoteEvent which the LocalScript can use to toggle time scale.
local SlowedTimeScale = 5
local SpeedTimePlayers = {}
local ToggleSpeedEvent = Instance.new("RemoteEvent")
ToggleSpeedEvent.Name = "ToggleSpeed"
ToggleSpeedEvent.Parent = game:GetService("ReplicatedStorage")
ToggleSpeedEvent.OnServerEvent:Connect(function(Player)
print("==========")
local TimeScale = 1
SpeedTimePlayers[Player.UserId] = if SpeedTimePlayers[Player.UserId] then nil else true
local SpeedTimePlayersCount = 0
for UserId, _ in SpeedTimePlayers do
SpeedTimePlayersCount += 1
end
TimeScale = if SpeedTimePlayersCount == 0 then 1 else SlowedTimeScale
if Player.Character then
if Player.Character:FindFirstChildOfClass("Humanoid") then
if SpeedTimePlayers[Player.UserId] then
game:GetService("CollectionService"):RemoveTag(Player.Character:FindFirstChildOfClass("Humanoid"), "TimeScaleWhitelist")
print(Player.Name .. " joined speed time!")
else
game:GetService("CollectionService"):AddTag(Player.Character:FindFirstChildOfClass("Humanoid"), "TimeScaleWhitelist")
print(Player.Name .. " left speed time!")
end
end
end
print("New time scale: " .. tostring(TimeScale))
game:GetService("ReplicatedStorage").TimeScaleUtilities:SetAttribute("TimeScale", TimeScale)
end)
game:GetService("Players").PlayerRemoving:Connect(function(Player)
SpeedTimePlayers[Player.UserId] = nil
end)
game:GetService("Players").PlayerAdded:Connect(function(Player)
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
if Humanoid then
game:GetService("CollectionService"):AddTag(Humanoid, "TimeScaleWhitelist")
end
Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character:WaitForChild("Humanoid")
if not Humanoid then return end
game:GetService("CollectionService"):AddTag(Humanoid, "TimeScaleWhitelist")
end)
end)
Insert a LocalScript into StarterPlayerScripts. The following code will allow the player to change the toggle the time scale between custom and normal.
local function ToggleSpeed(ActionName, InputState, InputObject)
if InputState == Enum.UserInputState.Begin or InputState == Enum.UserInputState.End then
game:GetService("ReplicatedStorage").ToggleSpeed:FireServer()
end
end
game:GetService("ContextActionService"):BindAction("ToggleSpeed", ToggleSpeed, true, Enum.KeyCode.LeftShift, Enum.KeyCode.ButtonR1)
Play Around With Settings
If you would like to change the settings for how much time changes, edit the SlowedTimeScale attribute attributed to the TimeScaleFramework folder in the ServerScriptService.
Conclusion
Congratulations! Hopefully now you will understand how to use my time scale framework!
You can see a basic working demonstration place for the time scale framework here:
Time Scale Demonstration
Please let me know if you have any issues with the framework & feel free to post what you’ve managed to create using it!