Screen stop shaking after player resets (dies)

Code:

local Char = script.Parent

local Hum = Char:WaitForChild("Humanoid")

local ShakeScreen = game:GetService("ReplicatedStorage").RemoteEvents.ShakeScreen

ShakeScreen.OnClientEvent:Connect(function(Duration)
	
	print("Starting screen shake.")
	
	local StartTime = tick()
	
	repeat
		
		task.wait()
		
		local EndTime = tick()
		
		local xOffset = math.random(-100,100) / 850
		
		local yOffset = math.random(-100,100) / 850
		
		local zOffset = math.random(-100,100) / 850
		
		Hum.CameraOffset = Vector3.new(xOffset,yOffset,zOffset)
		
	until EndTime - StartTime >= Duration
	
	Hum.CameraOffset = Vector3.new(0,0,0)
	
end)

The issue that I am dealing with is in the title.

1 Like

Have you tried checking if player is dead and then stopping the screen shake?

but i want it to continuously shake until its over completely

What does the server-side code look like?

Just a remoteevent that fires so the shake can start

yeah, just check if player is dead, if yes, end shake. If it’s not, continue with shaking

if Hum.Health == 0 then repeat task.wait() until Hum.Health > 0 end

I added this code after:

local zOffset = math.random(-100,100) / 850

But after it still doesn’t work

Change this

	repeat
		
		task.wait()
		
		local EndTime = tick()
		
		local xOffset = math.random(-100,100) / 850
		
		local yOffset = math.random(-100,100) / 850
		
		local zOffset = math.random(-100,100) / 850
		
		Hum.CameraOffset = Vector3.new(xOffset,yOffset,zOffset)
		
	until EndTime - StartTime >= Duration

into this

while EndTime - StartTime >= Duration do
	task.wait()
	local EndTime = tick()
	local xOffset = math.random(-100,100) / 850
	local yOffset = math.random(-100,100) / 850
	local zOffset = math.random(-100,100) / 850
	Hum.CameraOffset = Vector3.new(xOffset,yOffset,zOffset)
	if Hum.Health <= 0 then
		return

whats the difference between the two?

I believe while loop is more organized than repeat until, other than that pretty much nothing

Also just realized something, is the screen shaking healing or damaging the player? You might want to elaborate on what the actual problem is cause I assumed you wanted screen shake to end after character dies.

no healing or damage just a screen shake

Screen shake what? You want it to end after character dies or something?

I want it to continue even when the player is dead and end when the time is done

Problem: When the player dies the shaking stops early

Ah, alright, then the problem seems to be because the remoteEvent is in the StarterCharacter and not StarterPlayer folder, you’re gonna have to change where the local script that contains ShakeScreen event is located into StarterPlayer and change the code a bit to make sure the ShakeScreen works properly

why is this?

extra text…
abc123

Because when the character dies, the character will get deleted, also deleting the Local Script containing the remoteEvent, thus ending the script early

Does it matter if i put it in StarterPlayer or StarterPlayerScripts?

Yes it does, place it in StarterPlayerScripts. Wasn’t in studio to check what the actual name was

Why so?

extra text 123
abctest

If you’re talking about StarterPlayer and StarterPlayerScripts, well, I’m pretty sure that only local scripts work in the Script folders only, I haven’t actually tested that theory.

If you’re talking about StarterPlayerScript and StarterCharacterScript, then that’s because StarterCharacter refers to the Character of the player, as stated before, if the character dies, the local script gets deleted, and a new local script gets added for the new Character.
As for StarterPlayer, the StarterPlayer refers to the Player, not the Character of the Player, so when the character of the player dies, the Player doesn’t really get affected by the character’s death.