OnClientEvent not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear! This OnClientEvent working

  2. What is the issue? Include screenshots / videos if possible! The issue is that the OnClientEvent doesn’t work

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? Yeah, but there was nothing

Extra details: I’m trying to make a system where if you type a character’s name, your avatar becomes it.

The Script sending the Client Event:

local event = game.ReplicatedStorage.Events.ClassChange

event.OnServerEvent:Connect(function(plr, class, cam)
	local clone = class:Clone()
	clone.Parent = workspace
	clone.Name = plr.DisplayName
	plr.Character = clone
	event:FireClient(plr, clone)
end)

The LocalScript recieving the event:

local box = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character
local rS = game:GetService("ReplicatedStorage")
local event = rS.Events.ClassChange

function change()
	local class = rS.Classes:FindFirstChild(string.lower(box.Text))
	if class then
		event:FireServer(class)
		task.wait(1)
	end
end

function changeCam(char)
	workspace.CurrentCamera.CameraSubject = char.Humanoid
	print("a")
end

event.OnClientEvent:Connect(changeCam)
box.FocusLost:Connect(change)
1 Like

On line 3 on the server script it seems you put an extra “)” when connecting the function

Remote.OnServerEvent:Connect(function(Player)

end)

Oh, sorry about that, it was a typo while editing the scripts

The code seems correct. Can you double-check that there isn’t more than 1 remote event with the same name?

There’s only 1 remote event in ReplicatedStorage.Events.

Where are your scripts located?

Capture3421

Place your server script inside ServerScriptService

It’s in ServerScriptService, but it still doesn’t work.

The local script is in StarterGui, right?

Yes, it’s in the StarterGui service.

Can you show both of them again?

If you’re only playtesting this in studio, your Server scripts might not be initializing before your Client scripts are. Try adding a wait before you call exaFunc for the first time.

Capture3421
Capture3421

exaFunc is on event activation, in this case, the textbox.FocusLost event.

Hm. It’s likely we’d need to see your actual codebase (if not at least a detailed skeleton of its operation until the issue occurs) to assist with this.
Because otherwise, your code should work fine.

1 Like

Alright.

The LocalScript:

local box = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character
local rS = game:GetService("ReplicatedStorage")
local event = rS.Events.ClassChange

function change()
	local class = rS.Classes:FindFirstChild(string.lower(box.Text))
	if class then
		event:FireServer(class)
		task.wait(1)
	end
end

function changeCam(char)
	workspace.CurrentCamera.CameraSubject = char.Humanoid
	print("a")
end

event.OnClientEvent:Connect(changeCam)
box.FocusLost:Connect(change)

The Script:

local event = game.ReplicatedStorage.Events.ClassChange

event.OnServerEvent:Connect(function(plr, class, cam)
	local clone = class:Clone()
	clone.Parent = workspace
	clone.Name = plr.DisplayName
	plr.Character = clone
	event:FireClient(plr, clone)
end)

Ah. you’re trying to pass an Instance value, which Remotes are notoriously finicky with.
Try using this for your changeCam function.

function changeCam()
	workspace.CurrentCamera.CameraSubject = plr.Character.Humanoid
	print("a")
end

Could you check if it’s firing the server? Or the OnServerEvent function is playing? Before that your checking if the class exists and if it doesn’t it wont go through.

Well, it wouldn’t work either way, since the “a” doesn’t display on the console.