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 “”
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.
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)
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.