Problems with running a function in RenderStepped

Hello Everyone,

I have a simple problem I can’t seem to get my head around I am trying to make a camera
shake effect run only when the setting is turned on, I have tried replacing the local script I used with a module script, and it’s inside a local script which runs the function when the setting changes, the module script does the rest of the stuff like checking if it is set to true or false and does the camera shake effect, but whenever I try to change the setting, an error comes up saying

Problem

I am not 100% sure what is causing this but its probably just a function rewrite I’m still not 100% sure though and since this is in a renderStepped function it really does lag out my pc as it is giving many errors

here is the code

This is the LocalScript which handles the RenderStepped Function and sends a signal whenever the setting changes

local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local Interface = PlayerGui:WaitForChild("Interface")
local ClientSettings = Interface:WaitForChild("ClientSettings")
local runService = game:GetService("RunService")
local CamShakeModule = require(script.CameraShake)



ClientSettings.CamShake.Changed:Connect(function()
	runService.RenderStepped:Connect(CamShakeModule.CameraShakeFunction()) -- the script says this line is wrong
		-- runs the camera shake effect if the value is true
end)

Here is the module script which does the logic of the camera shake, the camera shake isn’t the problem, its the function not running the way it should

local module = {}

local runService = game:GetService("RunService")
local character = script.Parent.Parent
local humanoid = character:WaitForChild("Humanoid")
local FPval = game.Players.LocalPlayer.CameraMaxZoomDistance 
local CameraShakeValue = script.Parent.ShakeEnabled

function module.CameraShakeFunction()
	if CameraShakeValue.Value == true then
		--// this script only runs when you are in FP and walking/running
			local currentTime = tick()
			if FPval == 0.5 then
				if humanoid.MoveDirection.Magnitude > 0 then -- if you start walking
					local bobbleX = math.cos(currentTime *10) * humanoid.WalkSpeed/40
					local bobbleY = math.abs(math.sin(currentTime *10)) *  humanoid.WalkSpeed/40

					local bobble = Vector3.new(bobbleX, bobbleY, 0)

					humanoid.CameraOffset = humanoid.CameraOffset:Lerp(bobble, .25)
				elseif humanoid.MoveDirection.Magnitude == 0 then -- if you stop walking
					humanoid.CameraOffset = humanoid.CameraOffset * .75
				end
			end
		end
end



return module

I’m not sure to change the module script or to change the local script or how to execute the function I’m new to using module scripts and I am a bit confused, again the actual code of the camera shake isn’t the problem, the problem is how I wrote and implemented the script, I don’t know if I have to rewrite a function or change its placement I’m just a bit confused because the error states I didn’t pass a function but I did I’m just not sure.

Any help on this would be greatly appreciated,
Thanks for reading

1 Like

I’m not sure if this will fix it, but try this:

runService.RenderStepped:Connect(CamShakeModule.CameraShakeFunction)

Don’t include parameter brackets in functions like those

1 Like

The parentheses call the function, returning a value or nil. You want to pass the function which is why it works without the parentheses.

You essentially want to pass the pointer to the function rather than the result of running the function.

2 Likes

ill give it a try and see what happens

ah thanks, I understand now thanks so much

I sometimes forget certain rules, but with practice comes improvement

@MixedConscience and @BanTech

Both of your solutions have worked and now I know what I have done wrong, thanks a bunch for helping me out,

:slight_smile: