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)
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:
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
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
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
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)
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)