I need help to fix in my tycoon once a player leaves without claiming a tycoon. My Code :
game:GetService("Teams") local Players = game:GetService("Players") function GetTycoonFromPlayer(Player) for i, v in pairs(script.Parent.Tycoons:GetChildren()) do if v:IsA("Model") then if v.Owner.Value == Player then return v end end end return nil end local tycoonBackups = {} for i, v in pairs(script.Parent.Tycoons:GetChildren()) do tycoonBackups[v.Name] = v:Clone() if v:IsA("Model") then local Team = Instance.new("Team", game.Teams) Team.Name = v.Name Team.AutoAssignable = false Team.TeamColor = v.TeamColor.Value end end Players.PlayerAdded:Connect(function(Player) local PlayerCash = Instance.new("NumberValue", game.ServerStorage.PlayerMoney) PlayerCash.Name = Player.Name local PlayerOwnsTycoon = Instance.new("BoolValue", PlayerCash) PlayerOwnsTycoon.Name = "OwnsTycoon" end) Players.PlayerRemoving:Connect(function(Player) local PlayerCash = game.ServerStorage.PlayerMoney:FindFirstChild(Player.Name) if PlayerCash ~= nil then PlayerCash:Destroy() end local tycoon = GetTycoonFromPlayer(Player) local backup = tycoonBackups[tycoon.Name]:Clone() wait(1) backup.Parent = script.Parent.Tycoons end)
You have return nil
inside of your function GetTycoonFromPlayer
. So when this code is ran:
local tycoon = GetTycoonFromPlayer(Player)
tycoon will be nil IF any of the following are true:
script.Parent.Tycoons:GetChildren() returns a blank table (nothing is inside Tycoons)
The table returned from script.Parent.Tycoons:GetChildren() has no models NOTE: GetChildren() only returns a table of the immediate children, not descendants
If none of the models inside Tycoons contains a model that has a value property set to the player
On the very next line, this code is trying to get the Name
property of nil:
local backup = tycoonBackups[tycoon.Name]:Clone()
My guess is that Tycoons has folders with the models you need in those folders. As I said, :GetChildren() only returns the immediate children, not the children of the children. There are a couple of ways to get around this such as using :GetDescendants() which will return the entire hierarchy inside Tycoons as a table, or you can just use another for loop like this:
function GetTycoonFromPlayer(Player) --Capture the immediate children of Tycoons for i, v in pairs(script.Parent.Tycoons:GetChildren()) do --Capture each of those children's children for ii, vv in pairs(script.Parent.Tycoons[v.Name]:GetChildren()) do if vv:IsA("Model") then if script.Parent.Tycoons[v.Name][vv.Name].Owner.Value == Player then return v end end end end return -- don't need to specify nil here end
Since you could potentially return nil, this will prevent any action taken if nil is found:
local tycoon = GetTycoonFromPlayer(Player) if tycoon then --Checks to make sure tycoon isn't nil local backup = tycoonBackups[tycoon.Name]:Clone() wait(1) backup.Parent = script.Parent.Tycoons end
Hope this helps out.
[EDIT] I just re-read your post and noticed that it wasn't what I had guessed and that it was actually number 3 that was your problem. Thankfully I did cover that, you can ignore the for loop inside the for loop thing, but I'll keep it up as a reference in case you or anyone else runs into this problem.
When you index a value from a table, it has to be a number. Use the tonumber
function and convert the string into a number. Remember, if tycoon.Name
is not a number, it will throw an exception.
Here is what your code should look like
game:GetService("Teams") local Players = game:GetService("Players") function GetTycoonFromPlayer(Player) for i, v in pairs(script.Parent.Tycoons:GetChildren()) do if v:IsA("Model") then if v.Owner.Value == Player.Name then return v end end end return nil end local tycoonBackups = {} for i, v in pairs(script.Parent.Tycoons:GetChildren()) do tycoonBackups[v.Name] = v:Clone() if v:IsA("Model") then local Team = Instance.new("Team", game.Teams) Team.Name = v.Name Team.AutoAssignable = false Team.TeamColor = v.TeamColor.Value end end Players.PlayerAdded:Connect(function(Player) local PlayerCash = Instance.new("NumberValue", game.ServerStorage.PlayerMoney) PlayerCash.Name = Player.Name local PlayerOwnsTycoon = Instance.new("BoolValue", PlayerCash) PlayerOwnsTycoon.Name = "OwnsTycoon" end) Players.PlayerRemoving:Connect(function(Player) local PlayerCash = game.ServerStorage.PlayerMoney:FindFirstChild(Player.Name) if PlayerCash ~= nil then PlayerCash:Destroy() end local tycoon = GetTycoonFromPlayer(Player) local number = tonumber(tycoon.Name) local backup = table.find(tycoonBackups,tyccon.Name):Clone() wait(1) backup.Parent = script.Parent.Tycoons end)