How do we use RemoteEvents?

How do we actually use RemoteEvents :hear_no_evil: :hear_no_evil:?

2 Likes

You use a RemoteEvent to communicate to the Server to the Client and vice versa. To communicate with Server to Client you would have to something like this (only an example):

local myEvent = game:GetService("ReplicatedStorage").RemoteEvent  -- change path if needed
game.Players.PlayerAdded:Connect(function(player) -- every function which gets the player basically
	myEvent:FireClient(player) -- you need to have a player argument if it's Server to Client
end)

To receive you would use this OnClientEvent:

local myEvent = game:GetService("ReplicatedStorage").RemoteEvent  -- change path if needed
myEvent.OnClientEvent:Connect(function()
	-- code
end)

Now if you have to do Client to Server Communication you just switch around the Arguments so instead :FireClient() you use :FireServer(). YOU DON’T NEED A PLAYER ARGUMENT FOR THAT and ofcourse it will be .OnServerEvent

NOTE: There is also :FireAllClients(), no player argument is needed for, since you send it to all clients
2. NOTE: There is also BindableEvent, however they are used for Client-to-Client Communication and Server-To-Server Communication, so don’t forget that!

2 Likes

Umm well if i tried from a local script to script to run an animation like i added a script to the serverscriptservice and local script fires server to the script how to do that? actually i don’t know how we fire it from local script.Can i add the animations to the script by the local script when firing it?

1 Like

Yes you can, just like i said use :FireServer() and OnServerEvent
Now to get the Animation I think you need to use Instance.new(“Animation”) or you (can clone the animation and) parent it to the script

2 Likes

Well so i learn it better i will add an example

local Anim1 = Script.Animation
local Player = game.Players.localPlayer
local Character = Player.Character
local UserInput = game:GetService("UserInputService")
local Remote = Game.ReplicatedStorage.Remote1
local Load1 = Character.Humanoid:LoadAnimation(Anim1)
UserInput.InputBegan:Connect(Function(Key)
if key.KeyCode == Enum.KeyCode.E then
--How will we fire it? plus add Load1 with it
end
end)

and how to recieve them with the script in the serverscriptservice

One correction and here is the code:

 UserInput.InputBegan:Connect(Function(Key)
if Key.KeyCode == Enum.KeyCode.E then -- Key not key
 Remote:FireServer() -- i assume this is a local script
end
end)

and now in the script in server script service

-- ofc reference Remote
Remote.OnServerEvent:Connect(function(player) -- a player argument can be inserted here

end)

Thanks what about the animation?

Can’t you put the Animation inside the Server Script? and then basically do:

Remote.OnServerEvent:Connect(function(player) -- a player argument can be inserted here
	local Anim1 = script.Animation -- inside the Server Script in ServerScriptService
	local char = player.Character
	local hum = char:WaitForChild("Humanoid")
	local Load1 = hum:LoadAnimation(Anim1)
	Anim1:Play()
end)

well when i tried doing this it said that it cannot be loaded for some reasons

What exactly is the error message?

So, from what I know, you can use:

Remote:FireServer(Anim1)

then for the local script you can do

Remote.OnServerEvent:Connect(function(player, Anim1)
Anim1:Play

However, don’t rely on this because I’m not much of an expert.

cannot load animation provider service

i will try it out but it will take a moment cuz the script am working on is kinda big

uh, what, did you use that somewhere in the server?

what do you mean i actually added the animation somewhere after adding the whole script like Player.PlayerAdded then i added when the character is added then the animation

Actually its fired from the local script to the script

Can you show the entire script you wrote please? Working with only based on description and snippets is hard of knowing what the actual problem is.

The script from the server scriptservice was deleted while the new ones is about a flashlight but it now works locally so i want to show it to everyone so am going to edit this

--Variables
local Flashlight = game.ReplicatedStorage:FindFirstChild("FlashLightSystem"):FindFirstChild("Flashlight"):Clone()
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.Character.ChildAdded:Wait()
local UserInput = game:GetService("UserInputService")
local EquipService = Flashlight:FindFirstChild("Equipped/Unequipped").Value
local Part1 = Flashlight:FindFirstChild("Grip")
local Caneaquip = Flashlight:FindFirstChild("CanEquip/Unequip").Value
local Weld = Instance.new("Motor6D",Part1)
local Animations = game.ReplicatedStorage.Animations
local LoadEquip = Character:FindFirstChild("Humanoid"):LoadAnimation(Animations.Equipped)
local LoadEquip2 = Character:FindFirstChild("Humanoid"):LoadAnimation(Animations.Unequipped)
local Sound1 = Flashlight:FindFirstChild("Grip")["Flashlight On"]
local Sound2 = Flashlight:FindFirstChild("Grip")["Flashlight Off"]
local IsMobile = Player.PlayerGui:FindFirstChild("FlashlightGUI").Ismobile
local FlashlightGUI = Player.PlayerGui:FindFirstChild("FlashlightGUI")
------------------------------------------------
--Pc
--Functions
UserInput.InputBegan:Connect(function(Keycode)
	
	--Equip
	if Keycode.KeyCode == Enum.KeyCode.F and Caneaquip == true and EquipService == false then
		Flashlight.Parent = Player.Character
		Weld.Part0 = Character:FindFirstChild("Right Arm")
		Weld.Part1 = Part1
		Caneaquip = false

		LoadEquip:Play()
		Sound1:Play()
		wait(2)
		Caneaquip = true
		EquipService = true
	else
		--Unequip
		if UserInput.InputBegan and Keycode.KeyCode == Enum.KeyCode.F and Caneaquip == true and EquipService == true then

			Caneaquip = false
			LoadEquip:Stop()
			LoadEquip2:Play()
			Sound2:Play()
			wait(0.3)
			Player.Character.Flashlight:Destroy()
			wait(2)
			Caneaquip = true
			EquipService = false
			--Restarting
			script.Enabled = false
			script.Enabled = true
			-----------
		end
	end
end)


-----------------------------------------------------------------------
--Mobile
--Detection
if UserInput.TouchEnabled and UserInput.KeyboardEnabled == false  then
	FlashlightGUI.Enabled = true
	FlashlightGUI.FlashLightButton.MouseButton1Click:Connect(function(A)
		if Caneaquip == true and EquipService == false   then
			Flashlight.Parent = Player.Character
			Weld.Part0 = Character:FindFirstChild("Right Arm")
			Weld.Part1 = Part1
			Caneaquip = false

			LoadEquip:Play()
			Sound1:Play()
			wait(2)
			Caneaquip = true
			EquipService = true
		else
			if FlashlightGUI.FlashLightButton.MouseButton1Click and Caneaquip == true and EquipService == true  then
				Caneaquip = false
				LoadEquip:Stop()
				LoadEquip2:Play()
				Sound2:Play()
				wait(0.3)
				Player.Character.Flashlight:Destroy()
				wait(2)
				Caneaquip = true
				EquipService = false
				--Restarting
				script.Enabled = false
				script.Enabled = true
				-----------
			
			end
		end
	end)
end
-----------------------------------------------------------------------

I don’t see what the problem is?

1 Like

well the problem is when the player uses it it only appears to him