Having problems with player.UserId

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)


I would appreciate any help

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

1 Like

Also, the return nil is useless, because it will already return nothing if the tycoon isn’t found.

1 Like

So it fixed my UserId Issue, but it still doesn’t change anything in the attributes…

Are there still no attributes under Tycoon?

1 Like

Could you tell me what this prints?

Code:

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)

1 Like

I also printed 123 to make sure, but it doesn’t change the attributes… I don’t get it
image

can it be that I cloned my self for building reference? but the leaderboard still works

Are you sure that you’re looking at the right model?

1 Like

I also deleted my copy but that didn’t change anything

Did you read what I said? It seems like there are 3 different tycoons it could’ve chosen, so have you checked all of them?

1 Like

yes I checked all of them, numbering isn’t needed right?

What do you mean by numbering isn’t needed?

So you’re certain that not a single tycoon had your user id?

1 Like

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…

Sorry for the trouble, I was getting mad

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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.