How would I get The players to "freeze" when I press Q?

I want the Parts and the surrounding players to freeze (Or be anchored) when i press Q.

When I press Q, the parts get frozen but the player doesnt. I havent coded the Player freezing yet because im not exactly sure how i can accomplish that

There isn’t an exact issue, i just need some guidance on how i can get the Players and Isolate the player that presses Q so they don’t freeze, but other players do.

The most ive done as of now is get the parts to be anchored with the help of @LevonLight and Korvbagarens

@Korvbagarens helped me with my issue with toggling on and off ColorCorrection and LevonLight helped with trying to figure out how to anchor the parts.

Im still learning scripting and detailed and somewhat simple answers would be appreciated (I dont want someone to just give me the answer)

On the Client

local UIS = game:GetService("UserInputService")
local lighting = game.Lighting
local bool = false
local Workspace = game.Workspace

local TimeStop = game.ReplicatedStorage.TimeStop

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		TimeStop:FireServer()

	end
end)

On the Server

local UIS = game:GetService("UserInputService")
local lighting = game.Lighting
local bool = false
local Workspace = game.Workspace

local TimeStop = game.ReplicatedStorage.TimeStop
local Players = game.Players:GetPlayers()


TimeStop.OnServerEvent:Connect(function ()

	if bool then
		
		for i, v in pairs(Workspace:GetChildren()) do
			if v:IsA("Part") or v:IsA("SpawnLocation") then
				v.Anchored = true
				lighting.ColorCorrection.Saturation = -3
			end
		end
	else

		for i, v in pairs(Workspace:GetChildren()) do
			if v:IsA("Part") or v:IsA("SpawnLocation") then
				v.Anchored = false
				lighting.ColorCorrection.Saturation = 0
			end
		end
	end

	bool = not bool
end)

Thank you for your time! :grinning:

1 Like

Well first you’ll want to check the player varible. Server events automatically have a player argument sent through, this refers to the player who’s client sent the request. So for example:

someEvent.OnServerEvent:Connect(function(player)
    print(player.Name)
end)

This should help you isolate the player who stopped time.

In order to make other players freeze you’ll want to loop through the players list and freeze all the parts in their character:

for i,v in ipairs(game.Players:GetChildren()) do
    -- Do the anchoring in here
end

Hope this helps! Let me know if you have any questions

1 Like

Thank you!

So after i isolate the player i can freeze players who dont share the same name as the player?

Yes, you can do that if you look at my edited reply!

1 Like

I have a question about Ipairs, would using in pairs work the same?

Also im still a bit confused about how i’d format the coding, would it be something like:

for i, v in pairs(game.Players:GetChildren()) do
   if player.name = "Pyromxnia" then
   break
   end

No, so what you’d want to do is something like this:

This loop works the same as any other, since we’re looping through the players, v would be the player object. With the player object you have to get the Character object: player.Character.

Then you’ll want to do:

for i,v in ipairs(game.Players:GetChildren()) do
    if v ~= player then
         for j,k in ipairs(player.Character:GetChildren())
             if k:IsA("BasePart") then
                 k.Anchored = true
             end
         end
    end
end

You will have to edit this script a bit like adding the bool check for unanchoring and stuff, but hopefully this helps, again ask any questions you have

Thank you so much for the help!!

Im only confused about what “~” means lol

~ means “Does Not” so ~= is “Does Not Equal”

1 Like

One last thing!

I ran the script and it says theres an error, but i cant seem to locate it

local UIS = game:GetService("UserInputService")
local lighting = game.Lighting
local bool = false
local Workspace = game.Workspace

local TimeStop = game.ReplicatedStorage.TimeStop
local Players = game.Players:GetPlayers()


TimeStop.OnServerEvent:Connect(function (player)
	print(player.Name)
	
	if bool then
		
		for i, v in pairs(Workspace:GetChildren()) do
			if v:IsA("Part") or v:IsA("SpawnLocation") then
				v.Anchored = true
				lighting.ColorCorrection.Saturation = -3
			end
			for i, v in pairs(game.Players:GetChildren()) do
				if v ~= player then
					for i, v in pairs(player.Character:GetChildren()) do
						if v:IsA("BasePart") then
							v.Anchored = true
						end
					end
				end
			end
		end
	else

		for i, v in pairs(Workspace:GetChildren()) do
			if v:IsA("Part") or v:IsA("SpawnLocation") then
				v.Anchored = false
				lighting.ColorCorrection.Saturation = 0
			end
			for i, v in pairs(game.Players:GetChildren()) do --The error starts here
				if v ~= player then
					for i, v in pairs(player.Character:GetChildren()) do
						if v:IsA("BasePart") then
							v.Anchored = true
						end
					end
				end
			end
	bool = not bool
end --here is the actual error spot

Heres the output

Check if there’s any red lines anywhere, and match up statement starts with their ends.

The error means you didn’t end a statement on line 40, so check to make sure you did

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Lightning = game:GetService("Lighting")

local TimeStop = ReplicatedStorage.TimeStop

local Bool = false
local Debounce = true

TimeStop.OnServerEvent:Connect(function(LocalPlayer)
    if Debounce then
        Debounce = not Debounce

        for _, Player in ipairs(Players:GetPlayers()) do
            if Player ~= LocalPlayer then
                local Character = Player.Character or Player.CharacterAdded:Wait()

                for _, Child in ipairs(Character:GetChildren()) do
                    if Child:IsA("BasePart") then
                        Child.Anchored = Bool
                        Lightning.ColorCorrection.Saturation = Bool and -3 or 0
                    end
                end
            end
        end

        bool = not bool
        Debounce = not Debounce
    end
end)

how does this code work?
tried to make it so it works flawlessly

		for i, v in pairs(Workspace:GetChildren()) do
			if v:IsA("Part") or v:IsA("SpawnLocation") then
				v.Anchored = false
				lighting.ColorCorrection.Saturation = 0
			end
			for i, v in pairs(game.Players:GetChildren()) do
				if v ~= player then
					for i, v in pairs(player.Character:GetChildren()) do
						if v:IsA("BasePart") then
							v.Anchored = false
						end
					end
				end
			end
		end	
		
bool = not bool
			
			
end)

I looked through the part of the code that’s having errors, but i still cant seem to find the error. I added an extra end but that didnt seem to do anything

This looks like it’ll work, but why do we add two bool values?

I added a Debounce so even if the event fires again, it won’t run if the for loop isn’t done running

added it to be on the safe side

1 Like

You aren’t supposed to spoonfeed code, you gotta explain how it works and what you’re doing so the person actually learns and isn’t copy and pasting

3 Likes

Imo, I think you should rename your variables so they’re easier to understand - Bool is for determining whether time has stopped or not, so IsTimeStopped would be a better name. Debounce is fine, but I think CoolDown or IsRunningCheck would be better. It’d make reading the variables easier to digest imo.

Basically:

local IsRunningCheck = true
local IsTimeStopped = true

StopTimeRemote.OnServerEvent:Connect(function(Player)
    if not IsRunningCheck then -- Is the code currently running? If not,
    IsRunningCheck = true -- Prevents the below from being spammed
    
    if IsTimeStopped then -- Is the time currently stopped?
        IsTimeStopped = false -- Change the variable to false, so that it "unfreezes" the players
    else -- However, if time isn't current stopped,
        IsTimeStopped = true -- change the variable to true, so that it "freezes" the players
    end
    
    game.Lighting.ColorCorrection.Saturation = IsTimeStopped and -3 or 0 -- I moved this here because it doesn't need to be in the loop.
    
    for _, Target in ipairs(game.Players:GetPlayers()) do -- Iterate through the players, `GetPlayers` returns a table of the players currently in the game.
        if Target ~= Player and Target.Character ~= nil then -- Is the target not the player who fired the remote? And do they have a character?
            for __, Object in ipairs(Target.Character:GetDescendants()) do -- Iterate through the player's character, hats included.
                if Object:IsA("BasePart") then -- Was it a part? (Does the object inherit from BasePart?)
                    Object.Anchored = IsTimeStopped -- Anchors depending on whether `IsTimeStopped` was true or not
                end
            end
        end
    end
    
    wait(2) -- Wait 2 seconds,
    IsRunningCheck = false -- and allow another time stopped check to be ran again.
    end
end)

Links for what I used:
Instance:GetDescendants (roblox.com)
Players:GetPlayers (roblox.com)
Programming in Lua : 4.3.5 - Generic for loops
Debounce – When and Why (roblox.com)
Instance:IsA (roblox.com) Inheritance (object-oriented programming) - Wikipedia
Player.Character (roblox.com)

I hope this helps the OP :ok_hand:

1 Like

@tannnxr

your advice worked (Kinda)!!

I tested the game with a friend, and they didnt seem to be anchored when i pressed Q. infact when i pressed Q the script didnt run as i actually intended it to. The parts were unanchored but the colors didnt invert, and sometimes pressing Q did nothing at all

Just as a refresher this is the script i used

local lighting = game.Lighting
local bool = false
local Workspace = game.Workspace

local TimeStop = game.ReplicatedStorage.TimeStop
local Players = game.Players:GetPlayers()


TimeStop.OnServerEvent:Connect(function (player)
print(player.Name)
	if bool then

		for i, v in pairs(Workspace:GetChildren()) do
			if v:IsA("Part") or v:IsA("SpawnLocation") then
				v.Anchored = true
				lighting.ColorCorrection.Saturation = -3
			end
		end
		for i, v in pairs(game.Players:GetChildren()) do
			if v ~= player then
				for i, v in pairs(player.Character:GetChildren()) do
					if v:IsA("BasePart") then
						v.Ancored = true
					end
				end
			end
		end
	else

		for i, v in pairs(Workspace:GetChildren()) do
			if v:IsA("Part") or v:IsA("SpawnLocation") then
				v.Anchored = false
				lighting.ColorCorrection.Saturation = 0
			end
		end
		for i, v in pairs(game.Players:GetChildren()) do
			if v ~= player then
				for i, v in pairs(player.Character:GetChildren()) do
					if v:IsA("BasePart") then
						v.Ancored = false
					end
				end
			end
		end
	end

	bool = not bool
end)

yes you are right, didn’t find a need to do that though
didn’t want to change what they named it in the original post too much

only made minor changes to the variable names

imo Debounce isn’t even that bad, it’s an actual word that works well in this case

Is the LocalScript still the same as before?

Were there any errors or warnings in the output when the code was ran too?

EDIT

Sometimes it’s the simplest of things that we miss :laughing: I’m glad it’s working now :+1:

Heres the output:

When i run or play the game the output doesnt show anything