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.
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.
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")
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)
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)
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.
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.