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

How do you use math.random to do different functions in a tool?

Asked by
BIGM_7 4
2 years ago

I'm trying to create a tool that when you click with it in your hand has a 50/50 chance to give you 2 types of effects when I tested to see if this script would work I saw it had no errors but even when clicking it did nothing. Here is my script.

local tool = script.Parent local h = game.Players.LocalPlayer.Character.Humanoid local RandomNumber = math.random(1,2)

tool.Activated:Connect(function() if RandomNumber == 1 then h.MaxHealth = h.MaxHealth + 50 h.Health = h.Health + 50 end if RandomNumber == 2 then game.StarterGui.Blind.Enabled = true if h.Health == 0 then game.StarterGui.Blind.Enabled = false end end end)

0
Make sure the randomnumber is local and defined within the tool.Activated function, otherwise the number will always be the same no matter how many times you use the tool. KingDomas 153 — 2y

1 answer

Log in to vote
0
Answered by
pwx 1581 Moderation Voter
2 years ago

Seems you almost got the hang of it.

local Tool = script.Parent
local Players = game:GetService('Players')

Tool.Activated:Connect(function()
    local Player = Players:GetPlayerFromCharacter(Tool.Parent)
    local randomNumber = math.random(1,2) -- number between 1 and 2
    if Player.Character then
        if randomNumber == 1 then
            -- do stuff
        elseif randomNumber == 2 then
            -- do other stuff
        end -- number check
    end -- character check
end) -- onActivated
0
This isn't the best way, though - you should use an array of functions instead of using a ton of elseifs. sngnn 274 — 2y
0
Shut up Nerd DefinitelyNotTronix 19 — 2y
Ad

Answer this question