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

how to get around using (if) Value = (Number) while true do/then?

I'm trying to make a script where when a player joins, they get a random power. My idea on how to do this is if a random value is a certain number, then that is the power they get (Ex: (Power).Value = 45). The issue is, I can't do if statements because it makes the equal sign invalid. I can't do while true do statements either, as it just causes it to turn into the value. I'm bad with wording, so here is a script if it helps.

game.Players.PlayerAdded:Connect(function(player)
	   local Powers = Instance.new("Folder")
	   Powers.Name = "Powers"
    Powers.Parent = player
	 
	    local Earth = Instance.new("NumberValue")
	    Earth.Name = "Earth"
	    Earth.Parent = Powers
	 
    local Fire = Instance.new("NumberValue")
	    Fire.Name = "Fire"
	    Fire.Parent = Powers
	 
	    local Electricity = Instance.new("NumberValue")
	    Electricity.Name = "Electricity"
	    Electricity.Parent = Powers
	 
	local PowerTable = {}
	PowerTable[1] = Electricity
	PowerTable[2] = Earth
	PowerTable[3] = Fire
	PowerTable[math.random(1,3)].Value = 45
	 
Fire.Value = 45 while true do

	 game.StarterPlayer.StarterPlayerScripts.SkillActivation1.Disabled = true
	  game.StarterPlayer.StarterPlayerScripts.SkillActivation3.Disabled = true
	 game.StarterPlayer.StarterPlayerScripts.SkillActivation4.Disabled = true

end
 
Electricity.Value = 0 while true do
  game.StarterPlayer.StarterPlayerScripts.SkillActivation.Disabled = true
	    game.StarterPlayer.StarterPlayerScripts.SkillActivation3.Disabled = true
	    game.StarterPlayer.StarterPlayerScripts.SkillActivation4.Disabled = true

	 
	end
	 
Earth.Value = 45 while true do
	    game.StarterPlayer.StarterPlayerScripts.SkillActivation1.Disabled = true
	    game.StarterPlayer.StarterPlayerScripts.SkillActivation.Disabled = true
	    game.StarterPlayer.StarterPlayerScripts.SkillActivation3.Disabled = true

end
	 
	end)

2 answers

✓ Accepted answer

I think this might be what you are looking for.

game.Players.PlayerAdded:Connect(function (player)
	local Powers = Instance.new("Folder")
	Powers.Name = "Powers"
	Powers.Parent = player

	local Earth = Instance.new("NumberValue")
	Earth.Name = "Earth"
	Earth.Parent = Powers

	local Fire = Instance.new("NumberValue")
	Fire.Name = "Fire"
	Fire.Parent = Powers

	local Electricity = Instance.new("NumberValue")
	Electricity.Name = "Electricity"
	Electricity.Parent = Powers

	local PowerTable = { Electricity, Earth, Fire }
	PowerTable[math.random(1,3)].Value = 45

	if Fire.Value == 45 then
		game.StarterPlayer.StarterPlayerScripts.SkillActivation1.Disabled = true
		game.StarterPlayer.StarterPlayerScripts.SkillActivation3.Disabled = true
		game.StarterPlayer.StarterPlayerScripts.SkillActivation4.Disabled = true
	end

	if Electricity.Value == 45 then
		game.StarterPlayer.StarterPlayerScripts.SkillActivation.Disabled = true
		game.StarterPlayer.StarterPlayerScripts.SkillActivation3.Disabled = true
		game.StarterPlayer.StarterPlayerScripts.SkillActivation4.Disabled = true
	end

	if Earth.Value == 45 then
		game.StarterPlayer.StarterPlayerScripts.SkillActivation1.Disabled = true
		game.StarterPlayer.StarterPlayerScripts.SkillActivation.Disabled = true
		game.StarterPlayer.StarterPlayerScripts.SkillActivation3.Disabled = true
	end
end)

I recommend you take a refresher course on conditionals, conditional operators and possibly loops, as I think you've been confusing the assignment and equality operators. You might also want to reconsider using StarterPlayer. Scripts there are used for all players entering the game, not just the one that you want. This system also isn't very secure. An exploiter who can manipulate LocalScripts would be able to re-enable all of your skill scripts and use everything that is provided. I recommend you fix this by parenting the folder to ReplicatedStorage and giving it a unique identifier (such as the player's UserId). On the server side, parent the power script to the folder, and on the client side wait for a folder in replicated storage whose name is your UserId. Parent any LocalScripts in that folder to your own PlayerScripts. This ensures that each player only has access to their own powers.

2 comments

1

ah! Thanks. I should probably look into it more. I've been wondering what the use of == was.

0

Glad I could help :D

If you really want to continue using while loops, then make sure to break the loop when its finished running.

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