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)
math.random()
if
and elseif
statements checking what the random number is.
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
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.
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
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!