RemoteEvent for client does not work

Hello,

I do not know why but my OnClientEvent did not get info from the server (RemoteEvent):

Script (Server):

local rs = game:GetService("ReplicatedStorage")
local event = rs.Events.UI.MainMenu.Deploy

event.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local oldPosition = char.PrimaryPart
	
	local newCharacter = game:GetService("ReplicatedFirst")["Character Manager"].Character:Clone()
	local newAnimate = game:GetService("ReplicatedFirst")["Character Manager"].AnimatorCharacter:Clone()
	newAnimate.Name = "Animate"

	newCharacter.Name = plr.Name
	newCharacter:PivotTo(oldPosition.CFrame) 
	char:Destroy()
	plr.Character = newCharacter
	char = plr.Character
	newCharacter.Parent = workspace
	newAnimate.Parent = newCharacter
	
	local humanoid = newCharacter.Humanoid
	
	print(0)
	event:FireClient(plr, humanoid)
	print(1)
end)

Localscript (Client):

local rs = game:GetService("ReplicatedStorage")
local event = rs.Events.UI.MainMenu.Deploy

event.OnClientEvent:Connect(function(humanoid)
	print(2)
	workspace.Camera.CameraSubject = humanoid
	workspace.Camera.CameraType = Enum.CameraType.Custom
end)

Output:
image

Explorer (I tried to move the localscript in servserscriptservice but it did not work as well):

I found your issue, a script isn’t a remote event

local event = --Make this a remote event

What I mean by this is

local Event = game.ReplicatedStorage. -- Put the remote event after this

What are you talking about ???

Bruh it’s a remoteevent what are you saying???
image

Have you tried adding the plr, humanoid to the local script?

Yes but for the event OnClientEvent we do not need to add “plr” in args, only in the server side (to give a “target”).

Try to do it like this instead:

local rs = game:GetService("ReplicatedStorage")
local event = rs.Events.UI.MainMenu.Deploy

game.Players.PlayerAdded:Connect(function(plr) -- changed this line
	local char = plr.Character
	local oldPosition = char.PrimaryPart
	
	local newCharacter = game:GetService("ReplicatedFirst")["Character Manager"].Character:Clone()
	local newAnimate = game:GetService("ReplicatedFirst")["Character Manager"].AnimatorCharacter:Clone()
	newAnimate.Name = "Animate"

	newCharacter.Name = plr.Name
	newCharacter:PivotTo(oldPosition.CFrame) 
	char:Destroy()
	plr.Character = newCharacter
	char = plr.Character
	newCharacter.Parent = workspace
	newAnimate.Parent = newCharacter
	
	local humanoid = newCharacter.Humanoid
	
	print(0)
	event:FireClient(plr, humanoid)
	print(1)
end)

game.Players.PlayerAdded:Connect(function(plr) also gets the player when somebody joins the game.
OnServerEvent wont work unless you fire it in your localscript. But I guess it will also work if you replace the serverscript with the script above. It fires when the player joins the game and then it will fire the event to the client.

Player joins the game → Event fires to the client → The localscript should fire

I do not know that… :tipping_hand_man: it have to deploy the new character when the player click on a button :face_with_diagonal_mouth:

so if the player clicks a button the localscript is supposed to fire? If it should work like this, I can try to change the script again and send it

:face_with_diagonal_mouth: it’s the current script, the script I did…

If I didn´t get it wrong and your button is a Gui and not a button with a Clickdetector in the workspace then place this localscript in your ButtonGui:

Localscript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage.UI.MainMenu.Deploy

script.Parent.MouseButton1Click:Connect(function(Player)

print(1)
game.Workspace.Camera.CameraSubject = Player.Character.humanoid
game.Workspace.Camera.CameraType = Enum.CameraType.Custom

Event:FireServer() -- I dont think there should be anything inside FireServer()

end)

like this:
A

if this works delete your current Localscript and everything should be working fine

This script fires like this:
Player presses the Gui Button → It fires the Event to the Server → The Serverscript does the other stuff

oh and dont forget to replace the serverscript with this:

Serverscript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage.Events.UI.MainMenu.Deploy

Event.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local oldPosition = char.PrimaryPart

	local newCharacter = game:GetService("ReplicatedFirst")["Character Manager"].Character:Clone()
	local newAnimate = game:GetService("ReplicatedFirst")["Character Manager"].AnimatorCharacter:Clone()
	newAnimate.Name = "Animate"

	newCharacter.Name = plr.Name
	newCharacter:PivotTo(oldPosition.CFrame) 
	char:Destroy()
	plr.Character = newCharacter
	char = plr.Character
	newCharacter.Parent = workspace
	newAnimate.Parent = newCharacter

end)

I moved the content of the localscript in the localscript done with the UI yeah.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.