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.