Activating a local script using remote event

So i have a script that is in the ServerScriptService and if the proximity prompt has been held it fires an event:

local ShakeEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")




local ProximityPrompt = game.Workspace.Car.Body.ProxPromptTest.ProximityPrompt




ProximityPrompt.PromptButtonHoldBegan:Connect(function()
	while true do
	ShakeEvent:FireAllClients() --event that should get fired forever if the prompt is being held
		wait(0.1)
	end
	
	local Triggered = false
	print("Player is holding the prompt") -- Holding the prompt, hold duration infinite

	local abc = ProximityPrompt.PromptButtonHoldEnded:Once(function()
		task.wait()
		if Triggered then

			   game.Workspace.Door:Destroy() -- Ignore this
		else
			print("Player let go of prompt")
			ShakeEvent:FireAllClients(false) --Player let go of prompt
			                                          
		end    
	end)
	local abcd = ProximityPrompt.Triggered:Once(function()
		Triggered = true
	end)
end)

and a localscript that gets the event:

local ShakeEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
ShakeEvent.OnClientEvent:Connect(function(Time)

	local StartTime = tick()



	repeat

		wait()

		local EndTime = tick()

		if game.Workspace.Car.Body:FindFirstChild("ProxPromptTest") then

			local intensity = 25
			local range = 55

			local dist = game.Players.LocalPlayer:DistanceFromCharacter(game.Workspace.Car.Body:FindFirstChild("ProxPromptTest").Position)


			local cam = workspace.Camera

			local clamp = math.clamp(1 + (intensity - 0.1)*(1-(dist/range)), 0.1, intensity)

			cam.CFrame *= CFrame.Angles(math.random(1 - clamp, clamp) / 500, math.random(1 - clamp, clamp) / 500, math.random(1 - clamp, clamp) / 500)
			game:GetService('TweenService'):Create(game.Workspace.Camera,TweenInfo.new(0.2,Enum.EasingStyle.Quint,Enum.EasingDirection.Out,0,false,0),{CFrame = cam.CFrame*CFrame.Angles(0,0,math.rad(math.random(-clamp/2, clamp/2)))}):Play()
			game.Players.LocalPlayer.Character.Humanoid.CameraOffset = Vector3.new(math.random(1 - clamp, clamp / 20), math.random(1 - clamp, clamp / 20), math.random(0 - clamp, clamp / 15)) / 55

		else

			print('where is it')

		end


until EndTime - StartTime >= Time
	
	ShakeEvent:FireAllClients(false)
end)

Everything works fine but it keeps firing the event so the players screen keeps shaking, how do i make it contiously firing the event if the prompt is being held, and stop firing the event if the prompt is not being held anymore?

i just want that the screen stops shaking when the player has let go of the prompt and continues shaking when the player is holding again.

2 Likes

The error is in your ServerScript:

while true do
	ShakeEvent:FireAllClients() --event that should get fired forever if the prompt is being held
	wait(0.1)
end

There is nothing to tell to the loop to stop

I was testing and here is my code:

local ProximityPrompt = script.Parent.ProximityPrompt
local CanShake = false

ProximityPrompt.Triggered:Connect(function(plr)
	CanShake = true

end)

ProximityPrompt.TriggerEnded:Connect(function(plr)
	CanShake = false

end)

game["Run Service"].Heartbeat:Connect(function()
	if CanShake then
		print("Shaking")

	end
end)

This is the problem, it will prevent the rest of the code from running and yield, try to do this

ProximityPrompt.PromptButtonHoldBegan:Connect(function()
	local HoldEnded = false
	local Triggered = false
	print("Player is holding the prompt") -- Holding the prompt, hold duration infinite

	local abc = ProximityPrompt.PromptButtonHoldEnded:Once(function()
		task.wait()
		if Triggered then

			game.Workspace.Door:Destroy() -- Ignore this
		else
			print("Player let go of prompt")
			HoldEnded = true
			ShakeEvent:FireAllClients(false) --Player let go of prompt

		end    
	end)
	local abcd = ProximityPrompt.Triggered:Once(function()
		Triggered = true
	end)
	while HoldEnded == false do
		ShakeEvent:FireAllClients() --event that should get fired forever if the prompt is being held
		wait(0.1)
	end
end)

i am trying to fix this problem for so long, thank you so much it worked!