I've got a script which handles certain RemoteEvents; this is the code at the beginning to connect the events.
wait() game.Workspace.PlayerLeaveArmory.OnServerEvent:connect(function(plr, args) changeLoadout(plr, args[1]) end) print("Connection") game.Workspace.PartAnchor.OnServerEvent:connect(function(plr, args) anchorPart(args[1], args[2]) end) print("Connection") game.Workspace.ModifyLoadout.OnServerEvent:connect(function(plr, args) modify(plr, args[1], args[2], args[3], args[4]) end) print("Connection") game.Workspace.SelectLoadout.OnServerEvent:connect(function(plr, args) select(plr, args[1], args[2])end) print("Connection") function loadContainsType(load, weapon) local type = weapon.WeaponType.Value if type == "Guns" then . . . functions referenced above
The printouts of "Connection" almost never happen, but when they do, then the script works fine. However, sometimes I have to disable and re enable the script from the Server console in order to make it start working.
The script is pasted into game.Workspace and Enabled by a different script 1 second after all of the RemoteEvents are created and put in Workspace; right at the startup of a server.
As said in the question, this script only connects the events sometimes, and I'm not sure what's causing to work/not work. Thoughts?
Here's the script that pastes the other script and enables it:
--Ensures that a different script isn't setting up the events; in case a duplicate of the model that --Owns this script exists if game.Workspace:FindFirstChild("ArmoryEventsSetup") == nil then setup = Instance.new("BoolValue") setup.Name = "ArmoryEventsSetup" setup.Parent = game.Workspace --Sets up the client side of the Remote Events game.Players.PlayerAdded:connect(function(player) player:WaitForChild("Backpack") local event = Instance.new("RemoteEvent") event.Parent = player.Backpack event.Name = "PlayerEnterArmory" script.ArmoryClient:clone().Parent = player.Backpack end) wait(1) local event = Instance.new("RemoteEvent") event.Parent = game.Workspace event.Name = "PlayerLeaveArmory" local event = Instance.new("RemoteEvent") event.Parent = game.Workspace event.Name = "PartAnchor" local event = Instance.new("RemoteEvent") event.Parent = game.Workspace event.Name = "ModifyLoadout" local event = Instance.new("RemoteEvent") event.Parent = game.Workspace event.Name = "SelectLoadout" script.ArmoryServer:Clone().Parent = game.Workspace wait(1) game.Workspace.ArmoryServer.Disabled = false else script:remove() end
The script which creates those objects performs a wait(1)
while the script using the values starts almost immediately.
You're probably accessing the values before they are loaded (at least some of the time, since the total wait on both is approximately one second)
The wait
in the script creating the values is completely unnecessary, you should just remove it.
However, the script using the values should still explicitly pause until the other one is done. You can use WaitForChild
to wait for the last value to be created
workspace:WaitForChild("SelectLoadout");
should replace the wait()
at the top.