Problem with randomizing player tycoon

  1. What do you want to achieve? Keep it simple and clear!
    So i made an randomizer script which will give player random tycoon if ownerValue == nil or “”
  2. What is the issue? Include screenshots / videos if possible!
    Problem is that function get called 4998 times(idk why), before everything was PlayerAdded function, but i added different function for randomizing, because i couldn’t go to start of playerAdded function.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? Yes, i tryed to add wait in both functions, nothing help

Script:

local Players = game:GetService("Players")
local function RandomFunc(plr)
	local RandomNum = math.random(1,6)
	print(RandomNum)
	local v = workspace:FindFirstChild("GS"..RandomNum) or nil
	local OwnerVal = v:FindFirstChild("OwnerValue") or nil
	RandomFunc(OwnerVal)
	if OwnerVal == nil or OwnerVal == "" then
		OwnerVal.Value = plr.Name
	else
		return
	end
end
Players.PlayerAdded:Connect(function(plr)
	RandomFunc(plr)
end)

I see what is your problem here. You are running the function multiple times, here’s a better script I suggest you could use.
Note: Create a folder that has all the tycoons that can be claimed.

local players = game:GetService("Players")

local function assignRandomTycoon(folder, plr)
	for i, tyccon in pairs(folder:GetChildren()) do
		if tycoon.OwnerValue.Value == nil then
			tycoon.OwnerValue.Value = plr.Name -- string value
			
			break
		end
	end
end

players.PlayerAdded:Connect(function(player)
   assignRandomTycoon(workspace.FOLDERNAME, player)
end)

No works though, ownerValue.Value is nil(it just have nothing in value)

image
(In other value there is Bot1, Bot2 and etc.)

Is ownerValue a string value? or an object value.

StringValue (30 lettersssssssssss)

Ok i figured problem need to use

if tycoon.OwnerValue.Value == nil then -- instead of that

if tycoon.OwnerValue.Value == nil or "" then -- use that

Hm.
Does your script work fine?

Don’t use nil, just use “” (30 leters)

Yeah it works fine right now, thanks for help!

No problem! (30 leterssssssssssss)

Wait nvm it no works, i added not empty value to string value and it still changes value to plr name even if value not empty

image
In studio
image
In game

It is assigning your name. (30 leters)

Ok nvm fixed, i’m just dumb sorry

1 Like

There were Bot11 value, it wasn’t empty and it’s still assigned my name

1 Like
local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")

local Tycoons = Workspace.Tycoons --Tycoons folder.

local function OnPlayerAdded(Player)
	for _, Tycoon in ipairs(Tycoons:GetChildren()) do
		local Owner = Tycoon:FindFirstChild("Owner")
		if not Owner then continue end --Unlikely but just in case.
		if Owner.Value then continue end --Tycoon is already owned.
		Tycoon.Value = Player --Assign player as tycoon's owner.
	end
end

local function OnPlayerRemoving(Player)
	for _, Tycoon in ipairs(Tycoons:GetChildren()) do
		local Owner = Tycoon:FindFirstChild("Owner")
		if not Owner then continue end
		if not Owner.Value then continue end
		if Owner.Value ~= Player then continue end
		Owner.Value = nil --Player has left so the tycoon should no longer be owned.
		--Reset the tycoon here.
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)

Here’s an example script, I’d recommend using ObjectValues instead of StringValues.

Dude, topic already solved bruh

You kept replying to the thread with issues so I wasn’t sure.

Although you have leave script, so i will test it later, now i’m working on more harder things xD, anyways thanks for help