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

An Issue with Math.Random script in a tool. How to fix it?

I have an Issue with a tool I made, a guitar tool for future RP projects. As a person with no experience about LUA coding language, with tutorials in youtube and with some rookie scripters. This is the best I've done so far.

local player = game.Players.LocalPlayer
local localMouse = player:GetMouse()

local songs = {
	[1] = script.Parent.Handle.Acoustic1,
	[2] = script.Parent.Handle.Acoustic2,
	[3] = script.Parent.Handle.Acoustic3,
	[4] = script.Parent.Handle.Acoustic4
}

local song = songs[math.random(1,#songs)]
local GuitarON = false
local animation = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid"):LoadAnimation(script.Parent.Playing)

localMouse.Button1Down:Connect(function()
	if script.Parent.IsEquipped.Value == true then
		if GuitarON == false then
			GuitarON = true
			song:Play()
			animation:Play()
		elseif GuitarON == true then
			GuitarON = false
			song:Stop()
			animation:Stop()
		end
	end
end)








local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local h = char:WaitForChild("Humanoid")
local anim = h:LoadAnimation(script.Parent:WaitForChild("Handling"))

local tool = script.Parent

tool.Equipped:Connect(function()
	anim:Play()
end)

tool.Unequipped:Connect(function()
	anim:Stop()
	song:Stop()
	animation:Stop()
end)

The Issue is, when the tool has been equipped from where it has been placed, each time I click on mouse It plays the same music over and over. Even though, I did

local songs = {
	[1] = script.Parent.Handle.Acoustic1,
	[2] = script.Parent.Handle.Acoustic2,
	[3] = script.Parent.Handle.Acoustic3,
	[4] = script.Parent.Handle.Acoustic4
}

local song = songs[math.random(1,#songs)]

There are four songs in math.Random and only plays a single one each time clicked on mouse1.

What is the Issue that I do not see? Don't try to explain it to me because, I can barely cope with scripting explanations right now. I need visual examples to understand.

1 comment

0

You have to select a random song each time you activate the tool. Line 11 is a static assignment.

3 answers

✓ Accepted answer

When you use math.random() in the way that you are, you're using the default seed, as provided by Roblox. The seed essentially determines the outcome of the function, or maybe more appropriately, the randomness of it. The default seed, however, will always yield the same random numbers, every time it is run. Thus, you do get a random number the first time you ever run this program. However, following tests will yield the same results.

How do we overcome this issue, and achieve true randomness? We use our own seed. And what seed could we use, that is always changing, always different, thus yielding truly random numbers? How about time?

Time is always changing, so if we use it as a seed to generate random numbers, the numbers should always be different, as well. We can use the tick() function to achieve this.

So, whenever you need to generate truly random numbers, simply place this at the top of your script:

math.randomseed(tick())

Then, all your uses of math.random() will yield truly random numbers!


#Note:

I really haven't used Roblox in a good while. I'm aware that there is a new method for generating random numbers with the constructor Random.new(). Using this method, you may not have to create a new seed, like we did in the previous instruction. I recommend reading the wiki documentation for a full discussion on this constructor, to decide whether you care to use one or the other!

2 comments

0

Evaluate the code before proceeding with an answer, the issue has no relation to PRNG entropy.

0

https://scriptinghelpers.org/questions/102714/how-do-i-use-randomnew-tick-and-what-are-seeds#94843 view this response to learn more about what this answer is discussing.

Ok so the issue here is that since you added the line that picks out a song outside of the Button1Down function it will only choose one random song and it will stay at that song since nothing is telling it to change again.

What you need to do is move the line that picks out the song into the Mouse.Button1Down function so that way every time the players clicks then a new song will be chosen. So something like this:

local player = game.Players.LocalPlayer
local localMouse = player:GetMouse()

local songs = {
    [1] = script.Parent.Handle.Acoustic1,
    [2] = script.Parent.Handle.Acoustic2,
    [3] = script.Parent.Handle.Acoustic3,
    [4] = script.Parent.Handle.Acoustic4
}

local GuitarON = false
local animation = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid"):LoadAnimation(script.Parent.Playing)

localMouse.Button1Down:Connect(function()
    if script.Parent.IsEquipped.Value == true then
local song = songs[math.random(1,#songs)] --move it here so it picks out a new one with each click
        if GuitarON == false then
            GuitarON = true
            song:Play()
            animation:Play()
        elseif GuitarON == true then
            GuitarON = false
            song:Stop()
            animation:Stop()
        end
    end
end)








local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local h = char:WaitForChild("Humanoid")
local anim = h:LoadAnimation(script.Parent:WaitForChild("Handling"))

local tool = script.Parent

tool.Equipped:Connect(function()
    anim:Play()
end)

tool.Unequipped:Connect(function()
    anim:Stop()
    song:Stop()
    animation:Stop()
end)

Hopefully this helps :)

math.random(min,Max)

is more comon to get a lower number, if you want to loop throught song i reccomend this:

local songs = {
    [1] = script.Parent.Handle.Acoustic1,
    [2] = script.Parent.Handle.Acoustic2,
    [3] = script.Parent.Handle.Acoustic3,
    [4] = script.Parent.Handle.Acoustic4
}

local song
function GetSong()
	local songIndex
	if song == nil or songIndex == #songs then
		song = songs[1]
		songIndex = 1
	else
		if songIndex ~= #songs then
			song = songs[songIndex+1]
		end
	end
	return song
end

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