Can someone help me with my tycoon leaving script?

I have the script below, My goal is to reset the owner/worker value (which is an object value) when a player leaves. Along with resetting the cash the owner/worker has made. Note: I don’t want the tycoon to reset until both the Owner and Worker value are nil. Currently, my error is getting the script to recognize the owner, I’m not sure if everything under that work properly. Here is the directory of the Tycoon.

https://gyazo.com/9c3ebb16c68d17629f589737f4361989

The script below is being used in the script “CoreScript” (ignore the Draft)

game.Players.PlayerRemoving:connect(function(player)
	local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name)
	
	local tycoon = script.Parent.Tycoons
	local assignedowner = tycoon.Name.Owner.Value
	local assignedworker = tycoon.Name.Worker.Value
	
	if assignedowner then
		print("[SERVER] Owner value found, script is proceeding")
	end
	
	if assignedworker then
		print("[SERVER] Worker value found, script is proceeding")
	end
	
	if assignedowner == player then
		tycoon.OwnerCash.Value = 0
		assignedowner = nil
		print("[SERVER] Changed Tycoon Owner value to ".. assignedowner)
	else
		warn("[SERVER] Error changing value for assignedowner")
	end
	
	if assignedworker == player then
		tycoon.WorkerCash.Value = 0
		assignedworker = nil
		print("[SERVER] Changed Tycoon Worker value to ".. assignedworker)
	else
		warn("[SERVER] Error changing value for assignedworker")
	end
	
	if cashmoney ~= nil then
		cashmoney:Destroy()
	end
		
	if assignedowner ~= nil and assignedworker ~= nil then
		if assignedworker ~= nil then
			print("worker value = nil")
		elseif assignedowner ~= nil then
			print("owner value = nil")
		--print("OWNER: ".. assignedowner .. " WORKER: " .. assignedworker.."")
		local backup = tycoonBackups[tycoon.Name]:Clone()
		tycoon:Destroy()
		wait(1)
		backup.Parent = script.Parent.Tycoons
		end
	end
end)

Thank you for taking the time to read over this and helping in any way you can.

1 Like

Instead of using variables to define the owner and worker, try using a table, ex:

local tycoonStaff = {Worker = nil, Owner = nil}

–then to add data to the table you would do this…

game.Players.PlayerAdded:Connect(function(player)

tycoonStaff.Owner = player

print(tycoonStaff.Owner.Name)

end)

game.Players.PlayerRemoving:Connect(function(player)

if player == tycoonStaff.Owner then

tycoonStaff.Owner = nil

print(“Owner left.”)

–Your code

end

end)

this is to roughly give you an idea of what to do, if you need more in depth help just contact me on discord @Drac#5808.

Ohhh! This makes more sense thank you very much! If I have any questions I’ll message you on discord for sure!

1 Like

Also, quick question. How would I get the tycoon the player is assigned to using variables? I would use the table but I would be using the local tycoonstaff = {Worker = nil, Owner = nil} accross multiple scripts.

Every tycoon would have its own script with its own staff table and When someone claims it , the values update, when any player leaves the game it checks to see if that player was it’s owner.

Is there a way for me to assign it all in one script?

Yes , your table would have to look more like this

local tycoons = {tycoonOne = {Owner = nil, Worker = nil}, tycoonTwo ={Owner = nil, Worker = nil}}

Sorry for asking so many questions, I’m very new to tables, how would I find out which owner and worker is assigned to each player?