How to make a looping script fire after loading

Help.

  1. I want to make a camera shutter fire in a pattern so that it syncs with an animation.

  2. Whenever the game loads, the animation plays AFTER the game loads, but the script starts BEFORE the game loads. This causes the shutter to not sync up.

  3. I have looked for solutions on the dev hub, and I have found a way to make the NPCS w/ the camera’s parent move to workspace from ReplicatedStorage after the game loads (then it works), but this is tedious and I plan to make several NPCS.

Is there any way to make this easier?

looping script

while true do
	wait(4.18)
	script.Parent["Camera Surreal 35 Mm Shutter 1 (SFX)"]:Play()
	script.Parent.PointLight.Enabled = true
	script.Parent.Transparency = 0
	wait(0.50)
	script.Parent.Transparency = 1
	script.Parent.PointLight.Enabled = false
	wait(1.34)
	script.Parent.PointLight.Enabled = true
	script.Parent["Camera Surreal 35 Mm Shutter 1 (SFX)"]:Play()
	script.Parent.Transparency = 0
	wait(0.50)
	script.Parent.Transparency = 1
	script.Parent.PointLight.Enabled = false
	wait(0.61)
	script.Parent.PointLight.Enabled = true
	script.Parent["Camera Surreal 35 Mm Shutter 1 (SFX)"]:Play()
	script.Parent.Transparency = 0
	wait(0.50)
	script.Parent.Transparency = 1
	script.Parent.PointLight.Enabled = false
	wait(1.05)
	end

intended effect (done with replicated storage technique mentioned)

3 Likes

Well, I believe this should work

repeat task.wait() until game:IsLoaded()

while true do
	task.wait(4.18)
	script.Parent["Camera Surreal 35 Mm Shutter 1 (SFX)"]:Play()
	script.Parent.PointLight.Enabled = true
	script.Parent.Transparency = 0
	task.wait(0.50)
	script.Parent.Transparency = 1
	script.Parent.PointLight.Enabled = false
	task.wait(1.34)
	script.Parent.PointLight.Enabled = true
	script.Parent["Camera Surreal 35 Mm Shutter 1 (SFX)"]:Play()
	script.Parent.Transparency = 0
	task.wait(0.50)
	script.Parent.Transparency = 1
	script.Parent.PointLight.Enabled = false
	task.wait(0.61)
	script.Parent.PointLight.Enabled = true
	script.Parent["Camera Surreal 35 Mm Shutter 1 (SFX)"]:Play()
	script.Parent.Transparency = 0
	task.wait(0.50)
	script.Parent.Transparency = 1
	script.Parent.PointLight.Enabled = false
	task.wait(1.05)
end

I replaced all the waits with task.wait because task.wait is basically an improved version of wait

1 Like

Use KeyFrameMarker’s as well as GetMarkerReachedSignal. You can add markers for each keyframe in which you want the camera light to flash, then connect those markers to a single function which will turn on the pointlight, wait a moment, then turn it off. It’s a lot easier than what you’re trying to do here.

1 Like

Tried, and the shutter didn’t go off at all

1 Like

The animation was done by replacing the idle animation in the player Animate script and then placing the script in the NPC. The animation was also imported from blender.

That complicates things a bit, I think.

I’ve only worked with animations made using the roblox animation creation tool. It might be possible for you to import the animation from blender and create a normal AnimationTrack using it, then edit it using the animation editor. There, you could set up the markers and use the method I mentioned before.

If it isn’t possible to do this then there may not be a decent way to properly sync up the animation to that function. Although, I could be completely wrong.

game.IsLoaded() should detect when the game completely loads.

local cameraanimation = Instance.new("Animation")

cameraanimation.AnimationId = "rbxassetid://9605039457"

local cameraanimationtrack = script.Parent.Parent.Parent.Humanoid:LoadAnimation(cameraanimation)

cameraanimationtrack:Play()

cameraanimationtrack:GetMarkerReachedSignal("shutter01"):Connect(function()
	print("test")
end)

I’ve never heard of this stuff before, so this script might look like garbage to developers familiar with animation events, but everything works, however, nothing is printed. Why is this?

script is located in the camera, not local script

Everything in that script appears fine. If the function isn’t firing then you should go back and make sure you gave the KeyFrameMarker's the correct name.

Your markers should look like this, for example.

image

It has the correct name. Are there any other reasons? Studio settings, etc?

Actually, I think I see the issue with the code. You’re using Humanoid:LoadAnimation which has been deprecated in favor of Animator:LoadAnimation. If this doesn’t resolve your problem, then I’m not sure what the issue could be.

Tried Animator:LoadAnimation and it still didn’t work. Oh well

I would just say to manually do the markings yourself by setting wait() to it’s correctful timing, and maybe off-setting the seconds. I was having a problem with this too, but I was able to fix it (sort-of) by changing the wait() to game:GetService("RunService").Heartbeat:Wait() would work.

If I sounded confusing, let me know.

By off-setting the seconds, does that mean off-setting the seconds so that they match with the loading times? If so, I’m pretty sure that would be an issue due to differing loading times with players. I also tried manually timing the markings in the original script.

and also replacing the waits did this (warning: flashing lights)

Jesus… Probably can just be a task.wait. Also, off-setting it like having it be more early than the actual time.

The problem is that the looping script fires too early.

Why make the script so long? just do this

if game:IsLoaded then
for 1, 5, 1 do
script.Parent["Camera Surreal 35 Mm Shutter 1 (SFX)"]:Play()
	script.Parent.PointLight.Enabled = true
	script.Parent.Transparency = 0
	wait(0.50)
     
    end)
end

It’s long, because the flashes are all unique.

Then maybe add a wait() before the loop actually starts.

Adding a wait would still cause it to not sync up, because the animation fires after the loading and the script fires before. And, again, this wouldn’t work because loading times are different. If I set the camera script to fire after 60 seconds and the animation to fire after 60 seconds as well, what about low end devices that take more than 60 seconds to load? And if I set the wait for too long, high end devices will see the animation isn’t working.