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

Using Tables and random values: Problems?

Hello, I am making this game and its main genre is Health care and so on. Here is what i have done so far:

I made a model and named it "Sickness". It's parent is game (game.Sickness) and inside the Model there are 6 BoolValues. The name of the BoolValues are as shown below:

BoolValue 1 - Frostbite BoolValue 2 - Radioactvity BoolValue 3 - Nausea BoolValue 4 - Fatigue BoolValue 5 - Fever BoolValue 6 - Werewolfia

I also have a SickScript for these BoolValues, Each BoolValue has a different impact on the player. The SickScript is also in game (game.SickScript).

I have a main script inside workspace, This script clones the Sickness Model and SickScript into the player. Here is the main script code:

sicknessList = {game.Sickness.Frostbite, game.Sickness.Radioactivity, game.Sickness.Nausea, game.Sickness.Fatigue, game.Sickness.Fever, game.Sickness.Werewolfia,}
playerList = {}

function playerAdded(player)
	if player then
	for i,v in pairs (game.Players:GetPlayers()) do
	game.Sickness:clone().Parent = player
	game.SickScript:clone().Parent = player
	table.insert(playerList, v)
	
	local ld = Instance.new("IntValue")
	ld.Name = "leaderstats"
	ld.Parent = player
	
	local money = Instance.new("IntValue", ld)
	money.Name = "Cures"
	money.Value = 0		
	
	local level = Instance.new("IntValue", ld)
	level.Name = "Level"
	level.Value = 1	
	
	local xp = Instance.new("IntValue", ld)
	xp.Name = "XP"
	xp.Value = 0

if xp.Value == 100 then
	level.Value = level.Value + 1
	
if xp.Value == 200 then
	level.Value = level.Value + 1

if xp.Value == 300 then
	level.Value = level.Value + 1

end	end end end end end

function playerSickness()
	print 'PlayerSickness: Started'
	local player = game.Players:GetPlayers()[math.random(1, game.Players.NumPlayers)]
	local sickness = sicknessList[math.random(1, #sicknessList)].Value == true
	player.TeamColor = BrickColor.new("Bright green")
end

function health()
	print 'PlayerHealth: Started'
	for i,v in pairs(game.Players:GetPlayers()) do
			local player = v
			if player.Character then
			bl = {true, false}
			for i,v in pairs(bl)do
			if v == true then
			
			
			while true do
			player.Character.Humanoid.Health = player.Character.Humanoid.Health - 1
			wait(60)
			if v == false then
				break
end end end end end end end

game.Players.PlayerAdded:connect(playerAdded)

while wait() do
	wait(15)
	playerSickness()
	health()
	wait(15)
end

The first part clones the Sickness Model and script into the player and gives the player leaderstats. My main problem is selecting a random player and a random sickness from the Sickness Model and making its value true. But, I get no errors. Meaning this is right but nothing is happening.

Could you please fix the function "playerSickness" and make it select a random player and to make a random sickness inside the model a true value.

Thank you,

  • GingerBisucuit99

2 answers

✓ Accepted answer

Right now you have one global table, that references all sicknesses within the 'Sicknkess' model. Instead of altering the sicknesses copied to player, you are changing the sample model, located in 'game'.

So instead of having one table with all the sicknesses, you could simply directly change them within player. That'd look like:

function playerSickness()
	    print 'PlayerSickness: Started'
	    local player = game.Players:GetPlayers()[math.random(game.Players.NumPlayers)]
	    local sList = player.Sickness:GetChildren()
	    sList[math.random(#sList)].Value = true
	    player.TeamColor = BrickColor.new("Bright green")
end

While applying the fix, I also noticed:

local sickness = sicknessList[math.random(1, #sicknessList)].Value == true

So right now you are storing a boolean in 'sickness' variable, which doesn't change values actual state, so I changed it to:

sicknessList[math.random(#sicknessList)].Value = true

Thank you so much, It now works. My only problem now is changing the player properties when the BoolValue is true. Thanks anyway.

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