[Code in description] Roblox Freeze Your Character For A Duration

Published: 19 November 2023
on channel: sage
81
0

Open description for the code.

In this vid i just show the process of me coding a duration based character freezer.

I made a FreezeMovement module which is to be required by both client and server. The client does not have any use for what gets returned, but the module must be required in the client side to activate it, I'd require it in a localscript inside StarterPlayerScripts. The server gets returned a table that holds a function FreezeMovementFor which takes 2 parameters, Player and Duration.

All you have to do is make the server run the FreezeMovementFor function, for example:

FreezeMovment.FreezeMovementFor(Player,3) -- Freeze Player for 3 seconds

The code for the FreezeMovment module:
-------------------------------------------------------------------------------------------------------------------------------
local ContextActionService = game:GetService("ContextActionService")
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local FROZEN_ACTION_KEY = "freezeMovement"
local Tag = FROZEN_ACTION_KEY

local Threads = {}

if RunService:IsClient() then
local Actions = {
GetInstanceAddedSignal = function(Player)
ContextActionService:BindActionAtPriority(
FROZEN_ACTION_KEY,
function()
return Enum.ContextActionResult.Sink
end,
false,
Enum. ContextActionPriority.High.Value,
unpack(Enum.PlayerActions:GetEnumItems())
)
end,
GetInstanceRemovedSignal = function(Player)
ContextActionService:UnbindAction(FROZEN_ACTION_KEY)
end
}

local Player = Players.LocalPlayer

for Event,Action in Actions do
local Signal = CollectionService[Event](CollectionService,Tag)
Signal:Connect(function(Tagged)
if Tagged ~= Player then return end
Action(Tagged)
end)
end
end

if RunService:IsServer() then
local function Freeze(Player)
CollectionService:AddTag(Player,Tag)
end

local function Unfreeze(Player)
CollectionService:RemoveTag(Player,Tag)
end

Threads.FreezeMovementFor = function(Player,Duration)
if Threads[Player] then task.cancel(Threads[Player]) end
Threads[Player] = task.delay(Duration,Unfreeze,Player)
Freeze(Player)
end
end

return Threads
-------------------------------------------------------------------------------------------------------------------------------

Track: MANIA - Time Of Our Lives [NCS Release]
Music provided by NoCopyrightSounds.
Watch: http://ncs.lnk.to/TOULAT/youtube
Free Download / Stream: http://ncs.io/TOUL