Need help with when proximity prompt is triggered again

  1. What do you want to achieve?
    When the proximity prompt is triggered, player1 syncs their animation to player2. What I would like to add to the script is logic for if the prompt has already been triggered, then the animation for player1 will stop/return back to normal.

  2. What is the issue?
    I have the first part of the logic down, but not the second part.

Serverscript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local remoteEvent = ReplicatedStorage.RemoteEvent

local function onTriggered(player1, player2)
	print("Triggered on Server")
	remoteEvent:FireAllClients(player1, player2)
end

local function onCharacterAdded(character, player1)
	print("Character Added")
	local ownerPrompt = Instance.new("ProximityPrompt")
	ownerPrompt.ActionText = "Join"
	ownerPrompt.Name = "OwnerPrompt"
	ownerPrompt.HoldDuration = 1
	ownerPrompt.ObjectText = "Hold E to join dance!"
	ownerPrompt.RequiresLineOfSight = false
	ownerPrompt.Parent = character.PrimaryPart

	ownerPrompt.Triggered:Connect(function(player2)
		print("Prompt Triggered")
		onTriggered(player1, player2)
		
	end)
end

local function onPlayerAdded(player)
	print("Player Added")
	player.CharacterAdded:Connect(function(character)
		onCharacterAdded(character, player)
	end)
end

Players.PlayerAdded:Connect(onPlayerAdded)
print("Server Script Loaded")

ClientScript/LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

local function onClientEvent(player1, player2)
	print("Client Event Triggered")
	if player1.Character then
		local track = player1.Character.Humanoid.Animator:GetPlayingAnimationTracks()[1]

		if track and player2.Character then
			print("Playing Animation Track on Client")
			local timeStamp = track.TimePosition :: number
			local track = player2.Character.Humanoid.Animator:LoadAnimation(track.Animation)
			track:Play()
			track.TimePosition = timeStamp
			
		end
	end
end

remoteEvent.OnClientEvent:Connect(onClientEvent)
print("Client Script Loaded")

Simplified, would that be:
When the prompt is triggered, if the animation is already running then stop it, otherwise run some other code?

That’s what I was thinking, but I’m just not sure where exactly to implement it or what to write — I’m quite new to scripting!

For example, do I put it into the server or local script? I was thinking the serverscript cause that’s where the trigger logic is — but then then sync animation logic is in the LocalScript (which is in StarterPlayerScripts).

I know how to use = not but otherwise I’m not sure how to write out that logic.

You would put it on the server, so it shows for everyone.

Like this?

...

local function onTriggered(player1, player2)
	print("Triggered on Server")
	remoteEvent:FireAllClients(player1, player2)
end

local function onCharacterAdded(character, player1)
	print("Character Added")
	local ownerPrompt = Instance.new("ProximityPrompt")
	ownerPrompt.ActionText = "Join"
	ownerPrompt.Name = "OwnerPrompt"
	ownerPrompt.HoldDuration = 1
	ownerPrompt.ObjectText = "Hold E to join dance!"
	ownerPrompt.RequiresLineOfSight = false
	ownerPrompt.Parent = character.PrimaryPart

	ownerPrompt.Triggered:Connect(function(player2)
		print("Prompt Triggered")
		onTriggered(player1, player2)
		
		else 
	
	player2.Character.Humanoid.Animator:StopAllAnimations()
		
	end)
end

...

I mean, on the server you would put the code for stopping the animation

That is the serverscript. I’m just wondering if I’m doing it correctly.

Not working still :confused:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local remoteEvent = ReplicatedStorage.RemoteEvent

local idle = nil

local function onTriggered(player1, player2)
	print("Triggered on Server")

	remoteEvent:FireAllClients(player1, player2)
	idle = false
end

local function onCharacterAdded(character, player1)
	print("Character Added")
	local ownerPrompt = Instance.new("ProximityPrompt")
	ownerPrompt.ActionText = "Join"
	ownerPrompt.Name = "OwnerPrompt"
	ownerPrompt.HoldDuration = 1
	ownerPrompt.ObjectText = "Hold E to join dance!"
	ownerPrompt.RequiresLineOfSight = false
	ownerPrompt.Parent = character.PrimaryPart

	ownerPrompt.Triggered:Connect(function(player2)
		print("Prompt Triggered")
		if idle == nil then
			onTriggered(player1, player2)
			
		elseif idle == false then
			local track = player2.Character.Humanoid.Animator:GetAnimationTracks()[1]
			local idleTrack = player2.Character.Animate.idle.Animation1:LoadAnimation(track.Animation)

			idleTrack:Play()
		end
		idle = nil
	end)
end

local function onPlayerAdded(player)
	print("Player Added")
	player.CharacterAdded:Connect(function(character)
		onCharacterAdded(character, player)
	end)
end

Players.PlayerAdded:Connect(onPlayerAdded)
print("Server Script Loaded")