How to repeat while also letting the rest of the code run?

I have a code that makes the camera a certain way, but I want a specific value (custom FieldOfView) to be able to change and still update.

Here is what I currently have, there are no errors or anything but it doesn’t change the value.

Script inside ProximityPrompt

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local changeFOV = ReplicatedStorage.Events:WaitForChild("changeFOV")

script.Parent.Triggered:Connect(function(player)
	changeFOV:FireClient(player, 5)
end)

these two are to change the FOV while the game is running

LocalScript inside StarterGui

local FieldOfView = game.Players.LocalPlayer.PlayerScripts.FOV_Value
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local changeFOV = ReplicatedStorage.Events:WaitForChild("changeFOV")

changeFOV.OnClientEvent:Connect(function(zoom: number)
	print(zoom)
	FieldOfView.Value = zoom
end)

image [IntValue]

This is the main camera script that handles how the camera acts (Its an isometric camera)
local script in StarterCharacterScripts

wait(.5)
local zoom = 140
local FieldOfView = game.Players.LocalPlayer.PlayerScripts.FOV_Value.Value

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = game.Workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Custom

local RunService = game:GetService("RunService")
RunService.RenderStepped:Connect(function()
	Camera.FieldOfView = FieldOfView
	if Character then
		if Character:FindFirstChild("Head") then
			game:GetService("SoundService"):SetListener(Enum.ListenerType.ObjectCFrame, Character.Head)
			Camera.CFrame =
				CFrame.new(Vector3.new(Character.Head.Position.X + zoom, Character.Head.Position.Y + zoom, Character.Head.Position.Z + zoom), Character.Head.Position)
		end
	end
end)
1 Like

There are a couple of ways:

  1. Put your Code in a coroutine, Should help with Multi Tasking

  2. Put the loop at the Bottom of the Script, that way it doesn’t interfere with what’s going on above the script

1 Like

At the end of the entire code or the end of the RenderStepped?

When you create a variable it will either be a copy or a reference. Copies are created from base types and do not change unless you do so explicitly. References can change outside of your scripts.

Your FieldOfView variable is copying your FOV_Value.Value to get a reference try this. Since .Value is a base type (number, string, or boolean) it would be copied so we just remove that .Value.

local FieldOfView = game.Players.LocalPlayer.PlayerScripts.FOV_Value

-- use like this, you are getting the value from a reference
Camera.FieldOfView = FieldOfView.Value

References are created from Roblox types like Instance, NumberValue, Part, or created from tables. For example we can create a table and reference it, editing the reference will also edit the original variable

local original = { 100, 200 }
local reference = original

print (original[1]) -- 100
print (reference[1]) -- 100

reference[1] = 300
print(original[1]) -- 300 the original has changed via reference!
3 Likes

Typically you do when running Events and you want them to run before the Loop as anything below it will not run until its finished or broken, since you are using RunService.RenderStepped, it shouldn’t really be an issue since you can use an if statement

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