Hi, I’m trying to make a tycoon and assign a tycoon to a player…
So the problem is I cant use the player in the function but I can in the code underneath… and I’m smashing my desktop into oblivion, and pulling out my hair over this…
so in the main script player autocompletes with all the values like Name UserId and such…
but in the function it only autocompletes “player” no values
local Tycoons = game.Workspace:WaitForChild("Tycoons")
local function assignTycoon(player)
for _, Tycoon in Tycoons:GetChildren() do
if Tycoon:GetAttribute("Taken") then continue end
Tycoon:SetAttribute("Taken", true)
Tycoon:SetAttribute("UserId", player.UserId)
return Tycoon
end
return nil
end
game.Players.PlayerAdded:Connect(function(player)
local Tycoon = assignTycoon(player)
if not Tycoon then warn("Could not assign tycoon to "..player.Name) return end
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = 1000
money.Parent = leaderstats
end)
Are you asking about the type analysis? You can define the function like this:
local function assignTycoon(player: Player) -- This will define the variable as a 'Player' and provided class autocomplete
for _, Tycoon in Tycoons:GetChildren() do
if Tycoon:GetAttribute("Taken") then continue end
Tycoon:SetAttribute("Taken", true)
Tycoon:SetAttribute("UserId", player.UserId)
return Tycoon
end
return nil
end
The reason why the autocomplete works by default in the PlayerAdded event is because the event internally already has the types built-in
local Tycoons = workspace.Tycoons
local function assignTycoon(player)
for _, Tycoon in Tycoons:GetChildren() do
if Tycoon:GetAttribute("Taken") then continue end
Tycoon:SetAttribute("Taken", true)
Tycoon:SetAttribute("UserId", player.UserId)
print(Tycoon:GetAttribute("UserId"))
return Tycoon
end
end
(you should be using workspace instead of game.Workspace, and :WaitForChild isn’t needed, because server scripts only run when every preset item is loaded)
I fixed It!
I noticed I had the test folder in the tycoon folder, so it looked at the first one and got stuck… I didn’t have any fail states for that… So I moved it…
Yeah, I asked you if you’re certain that not a single tycoon had your user id, and you said yes lol.
Next time, please check what I actually tell you to check, and you should give the solution to @HugeCoolboy2007 because he actually solved your initial problem, not your own reply.