Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
lytew 99
4 years ago

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)
0
Also, the player's name doesn't have a child, humanoid. Geobloxia 251 — 4y
0
It does though, because the tool is inside of the character, and that references the humanoid inside of the character holding the tool. killerbrenden 1537 — 4y
0
oh Geobloxia 251 — 4y
0
I really don't know which one to choose ... everyone helped me lytew 99 — 4y
View all comments (2 more)
0
Choose which ever one helped you the most Geobloxia 251 — 4y
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. Geobloxia 251 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

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? lytew 99 — 4y
0
could you give me an example of how I use elseif lytew 99 — 4y
0
I did Geobloxia 251 — 4y
0
edited the answer.. Luka_Gaming07 534 — 4y
Ad
Log in to vote
0
Answered by
Geobloxia 251 Moderation Voter
4 years ago

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
0
Just for your information, I was here first. It just took me time to work on the script. Geobloxia 251 — 4y
0
thanks lytew 99 — 4y
0
You're welcome. :) Geobloxia 251 — 4y
Log in to vote
0
Answered by 4 years ago

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!

Answer this question