Shake function doesn't seem to work

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to make a small screen shake execute when a boolean value in the workspace is set to true, and a big screen shake to execute when a different boolean value in the workspace is set to true.

  1. What is the issue? Include screenshots / videos if possible!

The shake won’t work. It simply doesn’t do anything nor does it show anything in the output, including the printed message.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I used the print() to see where the problem could’ve went wrong. The print() did not work either. I can’t see anything wrong with the code so I’m stumped.

local player = game.Players.LocalPlayer
local hum = player.Character:WaitForChild("Humanoid")

local LightsFlicker = game.Workspace.LightsFlicker
local StructureFell = game.Workspace.StructureFell

local function exproshun()
	print("exproshun")
		local duration = math.random(20,41)*.1
		local intensity = math.random(16,21)*.1
		local snd = Instance.new("Sound")
		local vol = .35*intensity
		snd.SoundId = "rbxassetid://153274423"
		snd.Pitch = math.random(90,110) * .01
		snd.Volume = vol
		snd.Parent = script
		game.Debris:AddItem(snd, 3)
		local fin = tick() + duration
		while game:GetService("RunService").RenderStepped:wait() and (fin-tick() > 0) and hum.Health > 0 do
			snd.Volume = vol * (fin-tick())
			hum.CameraOffset = Vector3.new( 
				((math.random(-5,5)*.017) * (fin-tick())),
				((math.random(-5,5)*.017) * (fin-tick())),
				((math.random(-5,5)*.017) * (fin-tick()))
			)
		end
end

local function exproshunbig()
	print("exproshunbig")
	local duration = math.random(20,41)*.1
	local intensity = math.random(16,21)*.1
	local snd = Instance.new("Sound")
	local vol = .35*intensity
	snd.SoundId = "rbxassetid://153274423"
	snd.Pitch = math.random(90,110) * .01
	snd.Volume = vol
	snd.Parent = script
	game.Debris:AddItem(snd, 3)
	local fin = tick() + duration
	while game:GetService("RunService").RenderStepped:wait() and (fin-tick() > 0) and hum.Health > 0 do
		snd.Volume = vol * (fin-tick())
		hum.CameraOffset = Vector3.new( 
			((math.random(-5,5)*.03) * (fin-tick())),
			((math.random(-5,5)*.03) * (fin-tick())),
			((math.random(-5,5)*.03) * (fin-tick()))
		)
	end
end

hum.Died:connect(function()
	hum.CameraOffset = Vector3.new()
end)

while true do
	if LightsFlicker.Value == true then
		LightsFlicker.Value = false
		exproshun()
	end
	if StructureFell.Value == true then
		StructureFell.Value = false
		exproshunbig()
	end
end

How do I fix the script?

1 Like

Where are you initially setting the values for LightsFlicker and StructureFell?

I am unsure of why no error messages were displayed, but when I tested it on Studio, the script got timed out. This is because of the

 while true do 

It executes the code inside each cycle and hogs all of your game’s resources.
To avoid this, you can either add a waiting time to the loop so it doesn’t infinitely repeat itself each cycle.
Instead of “while true do”, you can use “while task.wait() do”.

But, based on the context of the script, the option above is far from ideal. Instead of periodically checking if the value of the BooleanValue is true each loop, you should be using a script signal.

The following block of code replaces the “while true do” (and its content):

--Changing to use script signals instead of checking the value each loop
LightsFlicker.Changed:Connect(function(value)
	if value then
		LightsFlicker.Value = false
		exproshun()
	end
end)

StructureFell.Changed:Connect(function(value)
	if value then
		StructureFell.Value = false
		exproshunbig()
	end
end)

Also, make sure the localScript is in an adequate script execution environment, such as StarterCharacterScripts.

2 Likes

Wdym by this?

With the current way localScripts work on Roblox, they can only run code if they are inside a limited set of objects, those objects being:

(Taken from the Documentation)

Note that regular scripts don’t have the same limitations but also cannot access client-only resources, such as the camera. In the context of this script, in order to access and modify values related to the LocalPlayer’s character and camera, it is required to be using a localScript rather than a regular script.
It is also important to place said script in the right location. As I mentioned previously, StarterCharacterScripts is a good example of the right location for localScripts, as every script inside this folder will be placed inside the LocalPlayer’s character everytime it spawns or respawns, which is ideal for your use case.

Examples
LocalScript inside StarterCharacterScripts


Hope this helps

Yea it is in one of those

I changed it to this but it still doesn’t work

How do I make the shake script work when there doesn’t seem to be anything wrong with it?

Update: The shake now works the first time but doesn’t when I call the function again. I also changed the functions into local functions

I just created a different function for every shake and it works now.

local player = game.Players.LocalPlayer
local hum = player.Character:WaitForChild("Humanoid")

local Events = workspace.Multiplayer:WaitForChild("Map").Events
local LightsFlicker = Events.LightsFlicker
local StructureFell = Events.StructureFell
local RocksFell= Events.RocksFell

local function exproshun1()
		local duration = math.random(20,41)*.1
		local intensity = math.random(16,21)*.1
		local fin = tick() + duration
		while game:GetService("RunService").RenderStepped:wait() and (fin-tick() > 0) and hum.Health > 0 do
			hum.CameraOffset = Vector3.new( 
				((math.random(-5,5)*.015) * (fin-tick())),
				((math.random(-5,5)*.015) * (fin-tick())),
				((math.random(-5,5)*.015) * (fin-tick()))
			)
		end
end

local function exproshun2()
	local duration = math.random(20,41)*.1
	local intensity = math.random(16,21)*.1
	local fin = tick() + duration
	while game:GetService("RunService").RenderStepped:wait() and (fin-tick() > 0) and hum.Health > 0 do
		hum.CameraOffset = Vector3.new( 
			((math.random(-5,5)*.015) * (fin-tick())),
			((math.random(-5,5)*.015) * (fin-tick())),
			((math.random(-5,5)*.015) * (fin-tick()))
		)
	end
end

local function exproshunbig1()
	local duration = math.random(20,41)*.1
	local intensity = math.random(16,21)*.1
	local vol = .35*intensity
	local fin = tick() + duration
	while game:GetService("RunService").RenderStepped:wait() and (fin-tick() > 0) and hum.Health > 0 do
		hum.CameraOffset = Vector3.new(
			((math.random(-5,5)*.03) * (fin-tick())),
			((math.random(-5,5)*.03) * (fin-tick())),
			((math.random(-5,5)*.03) * (fin-tick()))
		)
	end
end

local function exproshunbig2()
	local duration = math.random(20,41)*.1
	local intensity = math.random(16,21)*.1
	local vol = .35*intensity
	local fin = tick() + duration
	while game:GetService("RunService").RenderStepped:wait() and (fin-tick() > 0) and hum.Health > 0 do
		hum.CameraOffset = Vector3.new(
			((math.random(-5,5)*.03) * (fin-tick())),
			((math.random(-5,5)*.03) * (fin-tick())),
			((math.random(-5,5)*.03) * (fin-tick()))
		)
	end
end

hum.Died:connect(function()
	hum.CameraOffset = Vector3.new()
end)

local HowManyFlickers = 0

LightsFlicker.Changed:Connect(function()
	if LightsFlicker.Value == true then
        HowManyFlickers = HowManyFlickers + 1
		if HowManyFlickers == 1 then
			exproshun1()
		end
		if HowManyFlickers == 2 then
			exproshun2()
		end
	end
	LightsFlicker.Value = false
end)

StructureFell.Changed:Connect(function()
	if StructureFell.Value == true then
		exproshunbig1()
	end
	StructureFell.Value = false
end)

RocksFell.Changed:Connect(function()
	if RocksFell.Value == true then
		exproshunbig2()
	end
	RocksFell.Value = false
end)

It’s inadequate but it’ll work

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.