Instances Not Being Created

Im saving a player’s safezone values as boolean instance inside the player. A problem with it is that about half the time the instance just isn’t created. Im pretty sure the code is correct. I don’t know If the code is wrong please or if I should be storing the safezone values differently. Thanks :slight_smile:

The Code:

game.Players.PlayerAdded:Connect(function(plr)
	local safezoneval = Instance.new("BoolValue", plr)
	safezoneval.Name = "SafezoneVal"
	safezoneval.Value = true
end)
1 Like

The code itself seems in-and-of-itself working, so maybe your issue is syntax somewhere in another script? There is really no way of knowing for myself. Also, this is most likely a mistake on here, but:

Storing values inside the player might make them easier to access, but they aren’t exactly secure there. Storing them in replicatedstorage and accessing them from a remoteevent will keep them safe, though making it a little more difficult to call in some situations.

Additionally, another small thing that I noticed, and may cause performance issues, is your use of noting the parent of the instance inside the argument, as seen below.

The following code may cause an additional line, but will make everything run smoother:

local safezoneval = Instance.new("BoolValue")
	safezoneval.Name = "SafezoneVal"
	safezoneval.Value = true
	safezoneval.Parent = plr --Or hopefully in a serverstorage in the near future

If I try doing something like this:

_G.WhitelistPlayers = {}

game.Players.PlayerAdded:Connect(function(plr)
	table.insert(_G.WhitelistPlayers, plr)
end)

Will it work and will it cause performance issues?

I don’t get the point of that… It changes nothing since ur having the player object value. Anyways, for your script, just make sure that the player is here by writing if plr then.

The global array is an alternative to the object value.

Cause for some reason instance.new just isn’t working

Depending when/where you declare the connection it may not be created in time before the player is added. There may be some yielding code before it.

You can test this theory by sticking it in a script all by itself somewhere like ServerScriptService and it will work every time without fail.

What I mean is,

-- stuff here that yields, e.g. wait(100)
game.Players.PlayerAdded:Connect(function(plr)
--....
end)

The connection to act upon the PlayerAdded event is not added until all the code above it has run, so if the player joins the game before the connection is created, you won’t get a value instance added to the player.

Your modules that you require or the zone creation may yield? Worth checking those.

I see. In that case it should work every time. Where is the script located?

And yes I think this would work, though I avoid using the global table where possible. Avoiding solving an issue that, for all intents and purposes, should work reliably without fail might be ignoring a larger issue somewhere.

It seems to be an engine bug I just restarted my computer and it works now.

The yielding was a problem on another one of my object scripts though