How to tell a script the difference between a string and an instance?

Here is how my system works:
First train points are the amount of trains the player has spawned and allows the game to control the amount of train cars the player can spawn.
I want to create a script that when press a button on a train car it deletes it and gives a train point back to the original spawner.

I’ve already managed to make that part of the script work.
but now when the original spawner has left the game, the script does not work.

Btw when the player spawns the train car it is named like this: PlayerName … sTrain
here is the script:

local clickDetector = script.Parent.ClickDetector--The delete button
local traincar = script.Parent.Parent--The traincar

clickDetector.MouseClick:Connect(function()
	if traincar.Name ~= "Workspace" then--so it cant delete workspace
		local SplitMessage = string.split(traincar.Name, "sTrain")--getting the players name
		local spawner = SplitMessage[1]
		local players = game.Players
		spawner = players:FindFirstChild(spawner)
		if spawner == string then--Here is the problem. I have tried to use ==instance here too
			spawner.Backpack.TrainsSpawned.Value = spawner.Backpack.TrainsSpawned.Value - 1--giving the train point back
		end
		traincar:Destroy()
	end
end)

I believe that when the player has left the game the spawner variable (the player) stays as a string.
and the player is an instance. I have tried to tell the script if its an string to ignore trying to give the train point back, but I cant figure it out.

1 Like

If you’re trying to figure out the type of an object, this check may be handy.

if type(variable) == "string" then -- https://www.lua.org/pil/2.html
    -- todo
else -- definitely not a string
    -- todo
end

Replace variable with your variable name and you should have a working check.

1 Like

Mark the above reply as a solution so people don’t click on this forum when your issue is already solved, and to also help others that come with the same question or issue.

oh sorry I forget about that.
I don’t post very often.

1 Like