What's wrong with my script?

  1. What do you want to achieve?
    Trying to send the animationID to the server, but it’s not working.

Overall, I’m trying to make a two-player emote, so the player who uses the proximity prompt that’s within the other player’s HumanoidRootPart will then play the same animation that Player1 is playing.

I’m quite new to scripting so I really appreciate the help <3

  1. What is the issue?
    Certain prints within my script aren’t printing, so I’m assuming it’s due to things not firing or connecting. I’m not getting any output errors, so it’s difficult to know what is going on!

  2. What solutions have you tried so far?
    I’ve spent plenty hours working on a solution and have had no luck.

Screenshots:
Screenshot 2024-01-15 at 12.59.29 AM
Screenshot 2024-01-15 at 12.59.08 AM

ServerScript:

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

local replicatedStorage = game:GetService("ReplicatedStorage")
local getEmoteAnimationId = replicatedStorage.CrossCommunication:WaitForChild("GetEmoteAnimationId")


local function onPlayerAdded(player)
	player.CharacterAdded:Connect(function(character)
		-- Wait for the character to load
		character:WaitForChild("HumanoidRootPart")

		-- Create a ProximityPrompt for the owner player
		local ownerPrompt = Instance.new("ProximityPrompt")
		ownerPrompt.Name = "OwnerPrompt"
		ownerPrompt.ActionText = "Join"
		ownerPrompt.ObjectText = "Hold E to join dance!"
		ownerPrompt.Parent = character.HumanoidRootPart
		ownerPrompt.RequiresLineOfSight = false
		
		
		-- Handle the interaction for the owner player
		ownerPrompt.Triggered:Connect(function()
			print(player.Name .. " interacted with the prompt!")
			

			local emoteAnimationId = getEmoteAnimationId:InvokeClient(player)
			if emoteAnimationId then
				print("Received Emote AnimationId: " .. emoteAnimationId)
				-- Add your server-side logic using the received AnimationId (WORK IN PROGRESS)

			else
				print("Failed to receive Emote AnimationId.")
			end
		end)
	end)
end
-- Connect the function to the PlayerAdded event
Players.PlayerAdded:Connect(onPlayerAdded)

LocalScript:

local player = game.Players.LocalPlayer
local character = player.Character
repeat wait()
	character = player.Character
until character
local hum = character:WaitForChild("Humanoid")
local emote = hum:LoadAnimation(script.Parent.Emote)
local playing = false

-- Reference to the RemoteEvent
local replicatedStorage = game:GetService("ReplicatedStorage")
local showPrompt = replicatedStorage.CrossCommunication.ShowPrompt
local getEmoteAnimationId = replicatedStorage.CrossCommunication.GetEmoteAnimationId


local humanoid = character:FindFirstChild("HumanoidRootPart")
local prompt = humanoid:FindFirstChild("OwnerPrompt")

script.Parent.MouseButton1Click:Connect(function(playerWhoClicked)
	if playing == false then
		emote:Play()
		hum.WalkSpeed = 0
		hum.JumpPower = 0
		playing = true
		script.Parent.BackgroundColor3 = Color3.new(0.597253, 0.977401, 0.974533)
		
	
		local emoteAnimationId = script.Parent.Emote.AnimationId
		local result = getEmoteAnimationId:InvokeServer(emoteAnimationId)

		if result then
			print("Server acknowledged the prompt.")
		else
			print("Failed to send AnimationId to the server.")
		end
		
		showPrompt:FireServer(playerWhoClicked, prompt, true, true)
		

	elseif playing == true then
		
		-- Trigger the server event to hide the prompt
		showPrompt:FireServer(playerWhoClicked, prompt, false, true)
	
		prompt.Enabled = false
		
		emote:Stop()
		hum.WalkSpeed = 16
		hum.JumpPower = 50
		playing = false
		script.Parent.BackgroundColor3 = Color3.new(0.992157, 0.835294, 0.905882)
	end
end)

2 Likes

Screenshot 2024-01-15 at 1.05.51 AM

Another Screenshot, if it helps!

1 Like

I see that your code is only sending data “client → server” and “server → client”.

But there isn’t any line to receive these data.

To receive data from client to server = “OnServerEvent()” & “OnServerInvoke()”

To receive data from server to client = “OnClientEvent()” & “OnClientInvoke()”

1 Like

in the local script, try changing the repeat function to:

local character = Player.Character or Player.CharacterAdded:Wait()

also like bengali said, you dont have a way to accept the event

so, you could send the request with :FireServer() and accept with .OnServerEvent()
or :FireClient(player), and accept with .OnClientEvent()

also when you accept the event from the client i recommend checking it just in case, hackers can send whatever they like through the event, like numbers, strings, a nan value, and other things

1 Like

There have some errors in these scripts, if you want verify a animation, its not recommended to send the animations id through the local to the server, because a exploiter can change the animation id to play any animation that he wants.

About the RemoteFunction, RemoteFunction are used to check things in the server / local and receive the result of these check, then lets fix these scripts.

Server script:

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

local replicatedStorage = game:GetService("ReplicatedStorage")
local getEmoteAnimationId = replicatedStorage.CrossCommunication:WaitForChild("GetEmoteAnimationId")
--Table of the animations that exist in the game
local AnimationsID = {}

--This function will be called when the local script use InvokeServer
getEmoteAnimationId.OnServerInvoke = function(AnimationID)
	--Checking if the animation id exist in the table
	if table.find(AnimationsID, AnimationID) then
		print("This emote exist in the game.")
		--if the animation exist, if will return true (you can return anything that you want)
		return true
	else
		print("This emote doens't exist in the game.")
		--There will return nothing
	end
end

local function onPlayerAdded(player)
	player.CharacterAdded:Connect(function(character)
		-- Wait for the character to load
		character:WaitForChild("HumanoidRootPart")

		-- Create a ProximityPrompt for the owner player
		local ownerPrompt = Instance.new("ProximityPrompt")
		ownerPrompt.Name = "OwnerPrompt"
		ownerPrompt.ActionText = "Join"
		ownerPrompt.ObjectText = "Hold E to join dance!"
		ownerPrompt.Parent = character.HumanoidRootPart
		ownerPrompt.RequiresLineOfSight = false


		-- Handle the interaction for the owner player
		ownerPrompt.Triggered:Connect(function()
			print(player.Name .. " interacted with the prompt!")

			--Getting the AnimationId
			local emoteAnimationId = getEmoteAnimationId:InvokeClient(player)
			if emoteAnimationId then
				print("Received Emote AnimationId: " .. emoteAnimationId)
				-- Add your server-side logic using the received AnimationId (WORK IN PROGRESS)

			else
				print("Failed to receive Emote AnimationId.")
			end
		end)
	end)
end
-- Connect the function to the PlayerAdded event
Players.PlayerAdded:Connect(onPlayerAdded)

Local script:

local player = game.Players.LocalPlayer
local character = player.Character
repeat wait()
	character = player.Character
until character
local hum = character:WaitForChild("Humanoid")
local emote = hum:LoadAnimation(script.Parent.Emote)
local playing = false

-- Reference to the RemoteEvent
local replicatedStorage = game:GetService("ReplicatedStorage")
local showPrompt = replicatedStorage.CrossCommunication.ShowPrompt
local getEmoteAnimationId = replicatedStorage.CrossCommunication.GetEmoteAnimationId

local humanoid = character:FindFirstChild("HumanoidRootPart")
local prompt = humanoid:FindFirstChild("OwnerPrompt")

--This function will be called when the server script use InvokeClient
getEmoteAnimationId.OnClientInvoke = function()
	--Checking if the Emote and AnimationId exist
	if script.Parent.Emote and script.Parent.Emote.AnimationId then
		--Returning the AnimationId to the server.
		return script.Parent.Emote.AnimationId
	end
end

script.Parent.MouseButton1Click:Connect(function(playerWhoClicked)
	if playing == false then
		emote:Play()
		hum.WalkSpeed = 0
		hum.JumpPower = 0
		playing = true
		script.Parent.BackgroundColor3 = Color3.new(0.597253, 0.977401, 0.974533)

		if script.Parent.Emote.AnimationId then
			print("This animation id exist in local side!")
		else
			print("This animation id dont exist in local side!")
		end
		
		local emoteAnimationId = script.Parent.Emote.AnimationId
		local result = getEmoteAnimationId:InvokeServer(emoteAnimationId)

		if result then
			print("Server acknowledged the prompt.")
		else
			print("Failed to send AnimationId to the server.")
		end

		showPrompt:FireServer(playerWhoClicked, prompt, true, true)


	elseif playing == true then

		-- Trigger the server event to hide the prompt
		showPrompt:FireServer(playerWhoClicked, prompt, false, true)

		prompt.Enabled = false

		emote:Stop()
		hum.WalkSpeed = 16
		hum.JumpPower = 50
		playing = false
		script.Parent.BackgroundColor3 = Color3.new(0.992157, 0.835294, 0.905882)
	end
end)

Say to me if there have any error or bugs!

2 Likes