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

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

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)

1 comment

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.

1 answer

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

2 comments

0

This isn't the best way, though - you should use an array of functions instead of using a ton of elseifs.

0

Shut up Nerd

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