Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

This script only runs sometimes... why might that be?

Asked by
ipiano 120
9 years ago

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

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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.

0
The wait(1)'s were put in as a last ditch attempt to make something, anything work. I used to have the WaitForChild in there, one for each of the Events, but that didn't seem to be helping. I'll put it back, but I'm not hopeful. ipiano 120 — 9y
0
Of course, now that someone else has brought this option to my attention; it's begun to work, as is always the case. Thanks for affirming that this is correct option! ipiano 120 — 9y
Ad

Answer this question