How to fire a remote event

THE REASON I AM CLONING A LOCAL SCRIPT IS BECAUSE THIS IS A MODULE

Hello, I have a remote even and I’m trying to have a script that manually adds a local script to the player which recieves the remote event signal. Here is my code:

Script:

local remote = script.Parent.GravityTo0
remote.Parent = game.ReplicatedStorage

game.Players.PlayerAdded:Connect(function(plr))
     local Code = script.Parent.LocalScript:Clone()
     Code.Parent = plr
     remote:FireClient(plr) 
end)

Local Script:

local remote = game.ReplicatedStorage:WaitForChild("GravityTo0")

remote.OnClientEvent:Connect(function()
	print("Working") -- doesnt fire when remote:FireClient(plr) is called
	game.Workspace.Gravity = 0
	task.wait(2)
	game.Workspace.Gravity = 196.2
end)

So the problem is that the local script never recieves the event.

2 Likes

This happens in mere seconds, instead I suggest to use a RemoteFunction, and let the client send it and the server respond to it.
Else I suggest to save the parameters as attributes for the script.

My code is different from the examples provided and the event is called everytime it detects that they are flying so it wouldnt be “in mere seconds”

Try removing the unnecessary ‘)’ at the end.
game.Players.PlayerAdded:Connect(function(plr)

Also,

Your server script may not have enough time to identify the Remote Event as an Instance. I’d recommend using a Wait For Child function just incase.
local remote = script.Parent:WaitForChild("GravityTo0")

Actually it would as the client would need to

Wait for the child, so the client loading would take longer than the server sending the message.

1 Like

There is a

Task.wait(5)

in my original code in the beginning
(and it loops through players not playeradded event cause I know it wont work with the wait)

Then that is the reason PlayerAdded is not running.

^, also I do not understand the process of cloning the remote to place it into ReplicatedStorage.

Just have it placed in ReplicatedStorage without cloning it.

Playeradded is called as well as it loops through the players.

Here is my full script:

task.wait(5)
local plrs = {}

local remote = script.Parent:WaitForChild("GravityTo0"):Clone()
remote.Parent = game.ReplicatedStorage

local function MovementCheck(plr)
	while table.find(plrs, plr) do
		local Op = plr.Character:GetPivot().Position
		task.wait(2)
		local P = plr.Character:GetPivot().Position
		
		local mag = (Vector3.new(P.X,0,P.Z) - Vector3.new(Op.X,0,Op.Z)).Magnitude
		if mag >= 40 then
			plr.Character:PivotTo(CFrame.new(Op))
			plr.Character.Humanoid.WalkSpeed = 0
			task.wait(1)
			plr.Character.Humanoid.WalkSpeed = 16
		end
	end
	print("couldnt find plr")
end

local function FlyChecker(plr)
	print("added " .. plr.Name)
	while table.find(plrs, plr) do
		local OP = plr.Character:GetPivot().Position.Y
		task.wait(2)
		local P = plr.Character:GetPivot().Position.Y
		local mag = OP - P
		if mag >= 15 then
			remote:FireClient(plr)
		end
		print(OP)
	end
	print("couldnt find plr")
end

game.Players.PlayerAdded:Connect(function(plr)
	table.insert(plrs, plr)
	local Code = script.Parent.LocalScript:Clone()
	Code.Parent = plr
	task.spawn(MovementCheck, plr)
	task.spawn(FlyChecker, plr)
end)

for i,v in pairs(game.Players:GetPlayers()) do
	table.insert(plrs, v)
	local Code = script.Parent.LocalScript:Clone()
	Code.Parent = v
	task.spawn(MovementCheck,v )
	task.spawn(FlyChecker,v)
end

game.Players.PlayerRemoving:Connect(function(plr)
	table.remove(plrs, table.find(plrs, plr))
end)

this is a module script so I cant do that

What can’t you do? As far as I know, Module Scripts does not restrict you from calling Instances that weren’t cloned.

The script works. The thing that doesnt work is the local script.

Perhaps there is an issue with

remote:FireClient(plr)

if the local script was manually placed into the plr object and not starterplayerscripts

You can make this smaller

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

local function onPlayerAdded(player : Player) -- player handler
   ... -- code
end

for _, player in pairs(PlayerService:GetPlayers()) do onPlayerAdded(player) end
PlayerService.PlayerAdded:Connect(onPlayerAdded)
1 Like

okay but why doesnt the remote event work

I suggest that instead of using :FireClient to either add attributes to the local script or to use a RemoteFunction (which the client sends, the server receives and sends back and the client receives).

These are the only viable solutions other than having a wait, before firing the RemoteEvent.

This I suggested before:

1 Like

Why do you place the remote using the script instead of placing it manually before the playtest? This process could potentially mess up the WaitForChild function of your local script, which would infinitively yield.

dude its a module script not a game omg. the script plays while the game plays not before

I know what is and how does a module script works.

I did not completely understand that sentence, could you please rephrase it?

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