Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script Creates More Than One BoolValue In The Player When I Want Only One , Any Help?

So I'm Making A Game Like Miner's Haven Where You Can Place Items And I'm Making The Money And Bases And I Get A Error When Setting Which Base The Player Gets. It Doesn't Print A Error.

Script:

game.Players.PlayerAdded:Connect(function(plr)
	local Money = Instance.new("NumberValue")
	Money.Parent = plr
	Money.Name = "Money"
	Money.Value = 0
	for i,v in pairs(game.Workspace.Bases:GetChildren()) do -- Creates Multiple BoolValues For Each Of The Bases Not Only One.
		for g,h in pairs(game.Players:GetDescendants()) do
			if h ~=v.Name then
				local Own = Instance.new("BoolValue") 
				Own.Name = v.Name
				Own.Parent = plr
			end
		end
	end
end)

1 answer

✓ Accepted answer

You're creating a BoolValue for every descendant of game.Players whose Object is not the same as the base's Name per base; aka, you're creating a BoolValue for every child of game.Players, and their children, and so on.

Why not try something like this, with ObjectValue values:

-- create a table of available bases
local bases = {};
for a, x in pairs(game.Workspace.Bases:GetChildren()) do
	bases[x] = false;
end

game.Players.PlayerAdded:connect(function(player)
	local money = Instance.new("NumberValue", player);
	money.Name = "Money";
	money.Value = 0;
	-- find an open base
	for a, x in pairs(bases) do
		if(x == false)then
			-- we found an open base, so close the base and give the player an objectvalue referencing the part, then break out of the for loop
			bases[a] = true;
			local value = Instance.new("ObjectValue", player);
			value.Name = "base";
			value.Value = a;
			break;
		end
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	-- player is leaving, so open their base to be reused
	bases[player.base.Value] = false;
end)

3 comments

0

It doesn't change the value of the ObjectValue

0

Sorry, simple fix. Replace table.insert(bases, false); on line 4 with bases[x] = false;

0

Ok it works now, thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.