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

How can I make the user not freeze from Its own timefreeze?

Asked by 1 year ago

Hehehe I make funny J?o?j?o? ?g?a?m?e? World of trollge game

Anyway, I am making an ability for a morph which lets you time freeze, currently It works (I guess) but the problem Is that the user also gets frozen from Its own timefreeze (I also don't know If the bodyparts get unanchored or not)

Script:

local Tool = script.Parent
local Debounce = false

Tool.RemoteEvent.OnServerEvent:Connect(function()
    Tool.Parent.TIMESTOP:Play()
    wait(2)
    local TIMEFREEZE = Instance.new("Part",game.Workspace)
    TIMEFREEZE.Position = Tool.Parent.Torso.Position
    TIMEFREEZE.Shape = Enum.PartType.Ball
    TIMEFREEZE.Anchored = true
    TIMEFREEZE.CanCollide = false
    TIMEFREEZE.CastShadow = false
    TIMEFREEZE.Material = Enum.Material.Neon
    TIMEFREEZE.BrickColor = BrickColor.new("Neon orange")
    TIMEFREEZE.Size = Vector3.new(1,1,1)
    for i=0, 100 do
        TIMEFREEZE.Size = TIMEFREEZE.Size + Vector3.new(10,10,10)
        TIMEFREEZE.Touched:Connect(function(hit)
            local Humanoid = hit.Parent:WaitForChild("Humanoid")
            if Humanoid ~= Tool.Parent then
                hit.Anchored = true
                wait(10)
                hit.Anchored = false
            end
        end)
        wait()
    end
    game:GetService("Debris"):AddItem(TIMEFREEZE,10)
end)

1 answer

Log in to vote
0
Answered by
Opieky 0
1 year ago
Edited 1 year ago

The error is inside of the Touched event handler.

Humanoid, which is a humanoid object, is being compared with Tool.Parent, which is the player’s model, so this will never evaluate to false. However, accessing Tool.Parent‘s humanoid will not fix the issue either.

Since we want to differentiate between the player who invoked the time freeze and everyone else, we must compare using a property that is unique to each player—their names! The new implementation is as follows:

TIMEFREEZE.Touched:Connect(function(hit)
    --get the player who invoked the time freeze
    local Holder = Tool.Parent
    --get the player who was hit by the time freeze
    local Hit = hit.Parent
    --check if they’re both humanoids
    if Hit:FindFirstChild‘Humanoid’ and Holder:FindFirstChild‘Humanoid’ then
        --check if they are not the same player
        if Hit.Name ~= Holder.Name then
            --freeze the player
            hit.Anchored = true
            --wait 10 seconds
            task.wait(10)
            --unfreeze
            hit.Anchored = false
        end
    end
end)

EDIT:

Method 1: Anchoring HumanoidRootPart

You could try finding Hit's HumanoidRootPartand anchoring it to freeze the player:

if Hit.Name ~= Holder.Name then
    Hit:FindFirstChild'HumanoidRootPart'.Anchored = true
    task.wait(10)
    Hit:FindFirstChild'HumanoidRootPart'.Anchored = false
end

Pros: - Good performance (no loops, you could make it even more efficient by storing Hit:FindFirstChild'HumanoidRootPart' in a variable so it doesn't have to do the same operation twice) Cons: - Arms and legs will still be moving while player is frozen - After unfreezing, player may teleport forward a little

Method 2: Freeze Every Body Part

This method requires that we define a filter function to filter out the player's body parts from everything else.

--returns a table containing all t fields that satisfy f
local function filter(t, f)
    local nt = {}
    for _,v in ipairs(t) do
        if f(v) then
            table.insert(nt, v)
        end
    end
    return nt
end

Alright, now let's use it in the freeze block:

if Hit.Name ~= Holder.Name then
    --make a table consisting of Hit's freezable parts
    local freezed = filter(Hit:GetChildren(),function(field)
        return field:IsA'Part'
    end)
    --freeze every part
    for _,v in ipairs(freezed) do
        v.Anchored = true
    end
    task.wait(10)
    --unfreeze every part
    for _,v in ipairs(freezed) do
        v.Anchored = false
    end
end

Pros: - Will totally freeze the player - No jumping forward after being frozen! Cons: - I'd assume it's not as fast as the other, but I have no tested in with many players, although there are definitely ways to optimize this (like memoization) - For some reason, it will freeze the players for 18-19 seconds no matter what is passed into task.wait() and I can't figure out why

0
Note: I wrote this on mobile so there may be some issues with how the quotes register Opieky 0 — 1y
0
Good news and bad news, The good news It, It manages to fixes the player from freezing Itself, the bad news Is that It doesn't freeze the other players imnotaguest1121 362 — 1y
0
I've edited the response to include some potential solutions! I hope that fixes it Opieky 0 — 1y
Ad

Answer this question