Whats wrong with my script?

When i try to run my TimeStop script, it doesnt work as intended to work.

I havent changed anything to the script, and it worked perfectly fine yesterday. But ever since today it hasnt worked and im not sure whats wrong with the script since the output isnt showing any errors.

Heres the output
RobloxStudioBeta_ZEU3jGGFRe

Heres the code im using

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

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild('Humanoid')

local animation = script.Animation

local TimeStop = game.ReplicatedStorage.TimeStop

local AnimationTrack = humanoid:LoadAnimation(animation)

if not bool then
	bool = true
	UIS.InputBegan:Connect(function(input, chat)
		if chat then
			return	
		elseif input.KeyCode == Enum.KeyCode.Q then
			AnimationTrack:Play()
			humanoid.WalkSpeed = 0
			TimeStop:FireServer()
			wait(1)
			humanoid.WalkSpeed = 16
		end
	end)
end
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 (player)
	print(player.Name.."Has Stopped Time!")
	if bool == false then
		lighting.ColorCorrection.Saturation = -3
		for i, v in pairs(Workspace:GetChildren()) do
			if v.Name == "Rock" or v:IsA("SpawnLocation") then
				game.Workspace.Rain.RainScript.Disabled = true
				v.Anchored = true
			end
		end
		for i, v in pairs(game.Players:GetChildren()) do
			if v ~= player then

				for i, v in pairs(v.Character:GetChildren()) do
					if v:IsA("BasePart") then
						v.Anchored = true
					end
				end
			end
		end
	else
		lighting.ColorCorrection.Saturation = 0
		for i, v in pairs(Workspace:GetChildren()) do
			if v.Name == "Rock" or v:IsA("SpawnLocation") then
				game.Workspace.Rain.RainScript.Disabled = false
				v.Anchored = false
			end
		end
		for i, v in pairs(game.Players:GetChildren()) do
			if v ~= player then

				for i, v in pairs(v.Character:GetChildren()) do
					if v:IsA("BasePart") then
						v.Anchored = false
					end
				end
			end
		end
	end

	bool = true
end)

When i play the game, the script runs but only one time.

As shown im pressing Q multiple times (you can tell since the animation is playing) but nothing happens

Im still learning alot about scripting so a somewhat simple explanation would be greatly apricated!

thank you for your time :smiley:

I think this is the cause. Once bool turns to true, it won’t work anymore because of this line:

Are you trying to make a debounce?

1 Like

No, im trying to make it so its togglable

But ill try to fix that and see if it works! thank you

Not sure if this is actually allowed but i don’t think there’s a space between function and (player)

1 Like

I removed the space and it still didn’t work :sweat_smile:

I fixed it, but it still didnt work

I took python classes before and I remember hearing that if you add a for i = … loop inside ANOTHER for i = … loop then the inside loop’s i overrides the outer loop’s i. Not really sure if this also applies in lua pairs and the 2nd variable (v), but change the inner loop’s 2nd variable (v) to e or something and do the same with other nested loops

1 Like

You aren’t resetting the bool value back to false here. This causes it to run once.

Here is a fixed version. Just put bool = false wherever you stop the code.

if not bool then
	bool = true
	UIS.InputBegan:Connect(function(input, chat)
		if chat then
			return	
		elseif input.KeyCode == Enum.KeyCode.Q then
			AnimationTrack:Play()
			humanoid.WalkSpeed = 0
			TimeStop:FireServer()
			wait(1)
			humanoid.WalkSpeed = 16
            bool = false
		end
	end)
end

It happens over here too. You aren’t resetting the value back to false.

1 Like

It happens over here too. You aren’t resetting the value back to false.