Stated variable coming out as a nil value?

I have a Local Script set up to fire a Remote Event with a variable attached to it to send to the Server Script that captures the event. The script works almost 100% of the time, but I am seeing that the variable shows up as nil rarely, and I cannot recreate it. This is happening in two different scripts:

Local Script:

speciestag:FireServer("Egg")

Server Script:

speciestag.OnServerEvent:Connect(function(player,species)
    print(species)
	if species == "Egg" then
		rpname.Adornee = player.Character:FindFirstChildOfClass("Model"):WaitForChild("Middle")
	end
end)

nil

The other script that this happens with shows me an error. I am mostly unable to recreate this and have only seen it happen when I get bug reports about it:

Local Script:

morph:FireServer("Egg")

Server Script:

morph.OnServerEvent:Connect(function(player,species)
	repeat wait() until player.Character
	local size = nil
	local runvolume = nil
	local runsoundspeed = nil
	local pickup = false
	if species == "Egg" then
		size = .16
		runvolume = 0
		runsoundspeed = 1
		pickup = true
	end
    AddBird (player,species,size,runvolume,runsoundspeed,pickup)
end)

function AddBird (player,species,size,runvolume,runsoundspeed,pickup)
     if size < 0.5 then -- Error is here
		h:WaitForChild("BodyWidthScale").Value= 0.6
		h:WaitForChild("BodyDepthScale").Value= 0.6
    else
		h:WaitForChild("BodyWidthScale").Value= 1
		h:WaitForChild("BodyDepthScale").Value= 1
	end
--rest of script
end

Attempt to compare nil with number

Again, I am usually unable to recreate this and it only happens sometimes, but what would be the reason and what can I do to reduce this bug?

1 Like

Can you post the actual output message?

image
This shows up in the developer console sometimes, but not always.

Line 355
AddBird (player,species,size,runvolume,runsoundspeed,pickup)

Line 364
if size < 0.5 then

Place the AddBird command inside of the If statement… Even thought the statement is false it is still calling the AddBird function, just it is sending all of the information as nil.

morph.OnServerEvent:Connect(function(player,species)
	repeat wait() until player.Character
	local size = nil
	local runvolume = nil
	local runsoundspeed = nil
	local pickup = false
	if species == "Egg" then
		size = .16
		runvolume = 0
		runsoundspeed = 1
		pickup = true
        AddBird (player,species,size,runvolume,runsoundspeed,pickup)
	end
end)
1 Like