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

how do I make the script choose randomly between different properties of a humanoid?

I'm creating a script that gives a random effect to a player who eats a apple (speed, super jump, ...) The problem is that I don't know how to make the script choose randomly among these effects so that it is applied to the player script

function eat()	
	local playername = script.Parent.Parent.Name
	script.Parent.Handle.eatsound:Play()
local humanoid = playername.Humanoid
--now I don't know what to put here so he can choose a random effect to apply to the player (should I use Math.random?)


wait(1.4)
script.Parent:Destroy()
end
script.Parent.Activated:connect(eat)

6 comments

0

Also, the player's name doesn't have a child, humanoid.

0

It does though, because the tool is inside of the character, and that references the humanoid inside of the character holding the tool.

0

oh

0

I really don't know which one to choose ... everyone helped me

0

Choose which ever one helped you the most

0

LukaRoblox100 was trying to help you learn, instead of hurting you by giving the answer. I gave the answer but added comments and simplicity so you could understand. Killerbrenden gave you an advanced script but didn't really explain it.

3 answers

#What to do?

  1. Store a Random number between 1 and how many effects there are into a variable using math.random()
  2. Have a bunch of if and elseif statements checking what the random number is.
    • In each elseif or if statement have a other Property to be set. e.g:
if RandomNumber == 1 then
	humanoid.WalkSpeed = 100
elseif RandomNumber == 2 then
	humanoid.JumpPower = 50
-- etc..
end

#Final Script I'm not giving you a Script because i want you too learn how to script. If i just gave you a script you aren't going to learn anythng.

4 comments

0

right, interesting. I'm going to try to modify the script here, I just have one last question: what is the function of elseif?

0

could you give me an example of how I use elseif

0

I did

0

edited the answer..


local options = {1,2} -- Options for power

function eat()
	local plr = script.Parent.Parent -- defines player
	local choose = math.random(1, #options) -- randomizes pick
	script.Parent.Handle.eatsound:Play() -- players souond
	if choose==1 then --if choose is 1 it gives player
		-- a power
		plr.Humanoid.WalkSpeed = 70 --changes player's humanoid
		-- properties so it gives player power
		print("Player has gotten speed power")
		script.Parent:remove() -- so player can't use it again
	elseif choose ==2 then -- if choose is 2 then it
		-- gives player different power
		plr.Humanoid.JumpPower = 100 --changes player's humanoid
		-- properties so it gives player power
		print("Player has gotten jump power")
		script.Parent:remove() --so player can't use it again
	end
end

script.Parent.Activated:Connect(eat) -- connects up function


3 comments

0

Just for your information, I was here first. It just took me time to work on the script.

0

thanks

0

You're welcome. :)

You would want to make a List/Table of all the parts of a humanoid you want to be affected, and then use a math.random function to choose a random one and then apply it to their character.

Something like this.

local humProps = {"JumpPower","Health","WalkSpeed"} --// You Can Add More, These Are Just Basic Ones
local JumpPowerChange = 25 --// You Can Also Change This
local HealthChange = 100 --// And This
local WalkSpeedChange = 15 --// And This

local function eatApple()
	math.randomseed(tick())
	local randomHumProp = humProps[math.random(1,#humProps)]
	local playerChar = script.Parent.Parent
	script.Parent.Handle.EatSound:Play()
	local player = game:GetService("Players"):FindFirstChild(playerChar.Name)
	local character = player.Character or player.CharacterAdded:Wait()
	local hum = character:FindFirstChild("Humanoid")
	
	if randomHumProp == "JumpPower" then
		hum.JumpPower = hum.JumpPower + JumpPowerChange
	elseif randomHumProp == "Health" then
		hum.MaxHealth = hum.MaxHealth + HealthChange
		wait(0.1)
		hum.Health = hum.Health + HealthChange
	elseif randomHumProp == "WalkSpeed" then
		hum.WalkSpeed = hum.WalkSpeed + WalkSpeedChange
	end
	
	wait(1.4)
	script.Parent:Destroy()
end

script.Parent.Activated:Connect(eatApple)

This will always randomize which one will be applied, and feel free to add more to the list and be creative!

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