Value nil to everything?

Hello! I am attempting to make a simple script, a server script makes a value inside of the player upon joining and a client script will pick up that variable and will check it’s value. The issue is that the value is not able to be picked up for some reason, so I tried to just print the value’s name from the client script.

Here is the code:

Server code that creates variable:

game.Players.PlayerAdded:Connect(function(player)
	local scareValue = Instance.new("BoolValue", player)
	scareValue.Name = "ScareValue"
	scareValue.Value = false
end)

Client code inside of StarterPlayerScripts:

print(game.Players.LocalPlayer.ScareValue.Name)

Does anyone know a fix? Thanks for reading!

1 Like

My only assumption is that the LocalScript is running first, which is resulting as a nil value

You can try this inside the code:

local Player = game.Players.LocalPlayer
local Scare = Player:WaitForChild("ScareValue")

print(Scare.Name)

Basically, it’s running too fast for the server to even create the value ahead of time for the client script to print

1 Like

I see, I will try this. Such an easy fix, thank you!

It worked! Thank you so much, and have an amazing day!

1 Like

Just a tip, don’t use instance.new with parent argument.

You should do this:

game.Players.PlayerAdded:Connect(function(player)
	local scareValue = Instance.new("BoolValue")
	scareValue.Name = "ScareValue"
	scareValue.Value = false
	scareValue.Parent = player
end)

Rather than this:

game.Players.PlayerAdded:Connect(function(player)
	local scareValue = Instance.new("BoolValue", player)
	scareValue.Name = "ScareValue"
	scareValue.Value = false
end)

You can read about why here.