Script thinking that variable is still nil

Hello devs!

I am creating a RTS game right now but at the selection localscript, The variable that was supposed to set itself to the unit makes my code think that its still nil.

Here’s the script:

--Services and Mouse

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

--Modules & Remotes

local Modules = game.ReplicatedStorage.Modules
local Remotes = game.ReplicatedStorage.RemoteEvents

--Requires

local UnitManager = require(Modules.UnitManager)

--Script
local SelectedUnit = nil

Remotes.AddSelectedUnit.OnClientEvent:Connect(function(unit)
	local CheckifUnit = unit:FindFirstChild("isselected")
	if CheckifUnit then
		SelectedUnit = unit
	else
		warn("COULD NOT SELECT, NOT A UNIT.")
	end
	print(SelectedUnit)
end)

Mouse.Button1Down:Connect(function()
	if not SelectedUnit == nil then
		local mousepos = Mouse.Hit.p
		UnitManager.MoveUnit(SelectedUnit,mousepos)
	else
		print("No unit selected.")
	end
end)
2 Likes

Where exactly does the unit variable get defined?

Are you getting the warning message, or no?

I am getting the warning message.

That part of the code

[spoiler]characterlimit[/spoiler]

Oh, I know why.

Using OnClientEvent sets the first argument to the player that fired the event. So, you should make unit the second argument.

Change this to:

Remotes.AddSelectedUnit.OnClientEvent:Connect(function(player, unit)

image
I’m getting errors now

Well that might be an issue with the LocalScript that is firing the event.

Make sure it is sending an Instance (unit) and it’s not nil.

But i want to start it with no unit selected.

What should i place in there instead of nil?

Oh, then keep it as is. But, change the Script to this

Remotes.AddSelectedUnit.OnClientEvent:Connect(function(unit)
	local CheckifUnit = false
    if unit then
        CheckifUnit = unit:FindFirstChild("isselected")
    end
	if CheckifUnit then
		SelectedUnit = unit
	else
		warn("COULD NOT SELECT, NOT A UNIT.")
	end
	print(SelectedUnit)
end)

It is working but those debug errors came back

When i select a unit and press on the floor, it just prints no unit selected

That has something to do with your LocalScript.

You might want to make a new topic for that, as this one is solved.

I might not be the best help for your other problem (with your LocalScript) so maybe others in a new topic might help.

It wasn’t printing the warn, it was printing “No unit Selected”

Yeah, that’s on the client. It doesn’t have to do with your server script

I think I found your problem:

  1. The unary operator not has higher precedence than the variable SelectedUnit therefore it’s (not SelectedUnit) == nil.
  2. Because not returns a boolean, this section of code will always result in false as x == nil can only result in true if x is nil.

Because of all this you’d probably mean not (SelectedUnit == nil) so you might want to put a parenthesis around SelectUnit == nil to if it solves your issue, or use SelectedUnit ~= nil instead.

1 Like

It actually worked! Thank you so much! But, My move unit function seems to not be working correctly. But thanks!