I can't get this function to execute in viewmodel

My code is supposed to make a countdown timer on the watch but I’ve tried putting the code that does that in literally everywhere and it just won’t work.

It gives me errors like “infinite yield possible” or just wont even give me errors or warnings.

local rs = game:GetService("RunService")
local replicatedstorage = game:GetService("ReplicatedStorage")
local plr = game.Players.LocalPlayer
local cha = plr.Character or plr.CharacterAdded:Wait()
local cam = workspace.CurrentCamera

--// CREATE VIEWMODEL

local clone = game.ReplicatedStorage.viewmodel_storage.viewmodel_model:Clone()

local tick_sound = clone.right_arm.watch_model.screen.tick_sound

tick_sound:Play()

clone.Parent = workspace.viewmodel_workspace_storage

--// SWAY FUNCTIONS

local sensitivity = 1 -- how quick/snappy the sway movements are. Don't go above 2
local swaysize = 0.8 -- how large/powerful the sway is. Don't go above 2
local includestrafe = false -- if true the fps arms will sway when the character is strafing
local includewalksway = true -- if true, fps arms will sway when you are walking
local includecamerasway = true -- if true, fps arms will sway when you move the camera
local headoffset = Vector3.new(0,0,0) -- the offset from the default camera position of the head. (0,1,0) will put the camera one stud above the head.
local uis = game:GetService("UserInputService")
local tweenservice = game:GetService("TweenService")
repeat rs.Heartbeat:Wait() until script.Parent:IsA("Model") -- yield until character
local rootpart = cha:WaitForChild("HumanoidRootPart")
local humanoid = cha:WaitForChild("Humanoid")
local isrunning = false
local sway = Vector3.new(0,0,0)
local walksway = CFrame.new(0,0,0)
local strafesway = CFrame.Angles(0,0,0)
local jumpsway = CFrame.new(0,0,0)
local jumpswaygoal = Instance.new("CFrameValue")

humanoid.Running:Connect(function(speed)
	if speed <= .3 then
		isrunning = false
	else
		isrunning = true
	end
end)

--// HOW IT WORKS YAYAYAYA

stepped_con = game:GetService("RunService").RenderStepped:connect(function()
		if isrunning == true and includewalksway then
			walksway = walksway:lerp(
				CFrame.new(

					(0.07*swaysize) * math.sin(tick() * (2 * humanoid.WalkSpeed/4)),
					(0.07*swaysize) * math.cos(tick() * (4 * humanoid.WalkSpeed/4)),
					0

				)*
					CFrame.Angles(

						0,
						0,
						(-.03*swaysize) * math.sin(tick() * (2 * humanoid.WalkSpeed/4))

					)
				,0.2*sensitivity)
		else
			walksway = walksway:Lerp(CFrame.new(), 0.05*sensitivity)
		end
		--
		local delta = uis:GetMouseDelta()
		--
		if includecamerasway then
			sway = sway:Lerp(Vector3.new(delta.X,delta.Y,delta.X/2), 0.1*sensitivity)
		end
		--
		if includestrafe then
			strafesway = strafesway:Lerp(CFrame.Angles(0,0,-rootpart.CFrame.rightVector:Dot(humanoid.MoveDirection)/(20/swaysize)), 0.1*sensitivity)
		end
		--
		-- cframe the viewmodel
		local finalcf = (cam.CFrame*walksway*jumpsway*strafesway*CFrame.Angles(math.rad(sway.Y*swaysize),math.rad(sway.X*swaysize)/10,math.rad(sway.Z*swaysize)/2))+(cam.CFrame.UpVector*(-1.7-(headoffset.Y+(script.AimOffset.Value.Y))))+(cam.CFrame.LookVector*(headoffset.Z+(script.AimOffset.Value.Z)))+(cam.CFrame.RightVector*(-headoffset.X-(script.AimOffset.Value.X)+(-(sway.X*swaysize)/75)))
		clone:SetPrimaryPartCFrame(finalcf)
end)

--// ACTUAL FUNCTIONS

local animation_folder = script.animations_str
local userinputservice = game:GetService("UserInputService")

--// WATCH ANIMS

local watch_equip_anim = clone.Humanoid:LoadAnimation(animation_folder.watch.equip)
local watch_unequip_anim = clone.Humanoid:LoadAnimation(animation_folder.watch.unequip)
local watch_equipidle_anim = clone.Humanoid:LoadAnimation(animation_folder.watch.equip_idle)
local watch_unequipidle_anim = clone.Humanoid:LoadAnimation(animation_folder.watch.unequip_idle)

--// FLASHLIGHT ANIMS

local flashlight_equip_anim = clone.Humanoid:LoadAnimation(animation_folder.flashlight.equip)
local flashlight_unequip_anim = clone.Humanoid:LoadAnimation(animation_folder.flashlight.unequip)
local flashlight_equipidle_anim = clone.Humanoid:LoadAnimation(animation_folder.flashlight.equip_idle)
local flashlight_unequipidle_anim = clone.Humanoid:LoadAnimation(animation_folder.flashlight.unequip_idle)

--// WATCH

local watch_up = false
watch_equipidle_anim.Looped = true
watch_unequipidle_anim.Looped = true
flashlight_equipidle_anim.Looped = true
flashlight_unequipidle_anim.Looped = true
watch_unequipidle_anim:Play()
flashlight_unequipidle_anim:Play()

local sound_folder = script.sound_str

userinputservice.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		sound_folder.equip_ingen:Play()
		if watch_up == false then
			watch_up = true
			sound_folder.watch_equip:Play()
			tick_sound.Volume += 0.6
			watch_unequipidle_anim:Stop()
			watch_equip_anim:Play()
			watch_equipidle_anim:Play()
			game:GetService("TweenService"):Create(cam,TweenInfo.new(0.8, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {FieldOfView = 60}):Play()
		elseif watch_up == true then
			watch_up = false
			tick_sound.Volume -= 0.6
			game:GetService("TweenService"):Create(cam,TweenInfo.new(0.8, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {FieldOfView = 70}):Play()
			watch_unequip_anim:Play()
			watch_equipidle_anim:Stop()
			wait(0.5)
			watch_unequipidle_anim:Play()
		end
	end
end)


local flashlight_up = false

userinputservice.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		sound_folder.equip_ingen:Play()
		if flashlight_up == false then
			flashlight_up = true
			game:GetService("TweenService"):Create(clone.viewmodel_camera.flashlight_light_part.big_light,TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Brightness = 1}):Play()
			game:GetService("TweenService"):Create(clone.viewmodel_camera.flashlight_light_part.small_light,TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Brightness = 2}):Play()
			game:GetService("TweenService"):Create(clone.viewmodel_camera.flashlight_light_part.spot_light,TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Brightness = 0.4}):Play()
			sound_folder.flashlight_equip:Play()
			flashlight_unequipidle_anim:Play()
			flashlight_equip_anim:Play()
			flashlight_equipidle_anim:Play()
		elseif flashlight_up == true then
			flashlight_up = false
			game:GetService("TweenService"):Create(clone.viewmodel_camera.flashlight_light_part.big_light,TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Brightness = 0}):Play()
			game:GetService("TweenService"):Create(clone.viewmodel_camera.flashlight_light_part.small_light,TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Brightness = 0}):Play()
			game:GetService("TweenService"):Create(clone.viewmodel_camera.flashlight_light_part.spot_light,TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Brightness = 0}):Play()
			sound_folder.flashlight_equip:Play()
			flashlight_unequip_anim:Play()
			flashlight_equipidle_anim:Stop()
			wait(0.5)
			flashlight_unequipidle_anim:Play()
		end
	end
end)

local Text = workspace.viewmodel_workspace_storage:WaitForChild("viewmodel_model").right_arm.watch_model.screen.gui.counter_time.Text

function Timer(minutes, seconds)
	repeat
		wait(1)
		if seconds <= 0 then
			minutes -= 1
			seconds = 59
		else
			seconds -= 1
		end

		if seconds < 10 then
			Text = tostring(minutes)..":0"..tostring(seconds)
		else
			Text = tostring(minutes)..":"..tostring(seconds)
		end
	until minutes <= 0 and seconds <= 0 
end

Timer(2,0)

the clone is the viewmodel’s model, the viewmodel gets put in workspace, I tried putting it in the Camera and that didn’t work either. ignore the sway functions and userinputservice stuff, just the last part and the top part.

I’m assuming that the message pops up for line 167?
It’s basically telling you that it is taking a long time (possibly infinite) for it to find viewmodel_model, parented to workspace.viewmodel_workspace_storage.

While the game is running in Studio, can you please look in explorer and check if viewmodel_model is actually parented to workspace.viewmodel_workspace_storage?

it is, i tried it both local and server side and when I run both so it has to find it, it just gives me nothing in output and doesn’t work

OK, assuming that Infinite Yield is no longer a problem:

On line 167, you assigned Text to be the current value of counter_time.Text. However, after line 167 Text has no affiliation with counter_time.Text, so you can’t update the Text value and expect counter_time.Text to also update.

What you should instead do is assigning counter_time to a variable (e.g. “Label”) then in your script, instead of doing Text = ..., you should do Label.Text = ....

like this

local Label = clone.right_arm.watch_model.screen.gui.counter_time

function Timer(minutes, seconds)
	repeat
		wait(1)
		if seconds <= 0 then
			minutes -= 1
			seconds = 59
		else
			seconds -= 1
		end

		if seconds < 10 then
			Label.Text = tostring(minutes)..":0"..tostring(seconds)
		else
			Label.Text = tostring(minutes)..":"..tostring(seconds)
		end
	until minutes <= 0 and seconds <= 0 
end

Timer(2,0)

it worked thank you so much, i been struggling with this for 2 days

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