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

Why is math.random only giving me one response?

Asked by 5 years ago

The script plays an animation then it follows with another. The other animation is random between two animations. When I test it out the first animation plays then a second one comes but every time I play the animation the same one follows. It only randomizes it throughout when the player leaves and rejoins the game, but when the player dies everything stops working until the player rejoins the game.

--Finds Player--
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer 
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid") 


--Finds Animations--
local Animation = Instance.new("Animation") 
Animation.AnimationId = "rbxassetid://2867339187" 
local AnimationR = Instance.new("Animation") 
AnimationR.AnimationId = "rbxassetid://2867339668"
local AnimationL = Instance.new("Animation") 
AnimationL.AnimationId = "rbxassetid://2867395453"
local Animations = {AnimationL,AnimationR}
local track = Humanoid:LoadAnimation(Animation)
local track2 = Humanoid:LoadAnimation(Animations[math.random(#Animations)])


--Just finding the Particles--
local particle = Character:WaitForChild("LeftUpperArm"):WaitForChild("ParticleEmitter") 
local particle1 = Character:WaitForChild("RightLowerArm"):WaitForChild("ParticleEmitter") 
local particle2 = Character:WaitForChild("RightUpperLeg"):WaitForChild("ParticleEmitter") 
local particle3 = Character:WaitForChild("UpperTorso"):WaitForChild("ParticleEmitter")
local particle4 = Character:WaitForChild("LowerTorso"):WaitForChild("ParticleEmitter")


--Funtion That Starts Animation and Enables the Particles--
UserInputService.InputBegan:Connect(function(input, proc) 
    if not proc and input.KeyCode == Enum.KeyCode.T then
        particle.Enabled = true 
        particle1.Enabled = true
        particle2.Enabled = true 
        particle3.Enabled = true
        particle4.Enabled = true
        track:Play() 
        wait(0.35) 
        track:Stop()
        track2:Play()  --Animaton Follows--
        wait(0.45)
        track2:Stop()
        particle.Enabled = false
        particle1.Enabled = false
        particle2.Enabled = false
        particle3.Enabled = false
        particle4.Enabled = false
    end 
end)
0
Bump songboy50 77 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

This is due to the fact that you're defining your math.random() outside of your function, which means it will always be the same.

Here's an example..

local Random = math.random(1,5)
print(Random)
print(Random)

Returns >> 2 Returns >> 2

Where if we define it inside our function, it'll make a random "value" each time.

function randomNumber()
    return math.random(1,5)
end

print(randomNumber())
print(randomNumber())

Returns >> 4 Returns >> 2

To fix your code do this,

--Finds Player--
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer 
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid") 


--Finds Animations--
local Animation = Instance.new("Animation") 
Animation.AnimationId = "rbxassetid://2867339187" 
local AnimationR = Instance.new("Animation") 
AnimationR.AnimationId = "rbxassetid://2867339668"
local AnimationL = Instance.new("Animation") 
AnimationL.AnimationId = "rbxassetid://2867395453"
local Animations = {AnimationL,AnimationR}
local track = Humanoid:LoadAnimation(Animation)

--Just finding the Particles--
local particle = Character:WaitForChild("LeftUpperArm"):WaitForChild("ParticleEmitter") 
local particle1 = Character:WaitForChild("RightLowerArm"):WaitForChild("ParticleEmitter") 
local particle2 = Character:WaitForChild("RightUpperLeg"):WaitForChild("ParticleEmitter") 
local particle3 = Character:WaitForChild("UpperTorso"):WaitForChild("ParticleEmitter")
local particle4 = Character:WaitForChild("LowerTorso"):WaitForChild("ParticleEmitter")


--Funtion That Starts Animation and Enables the Particles--
UserInputService.InputBegan:Connect(function(input, proc) 
    if not proc and input.KeyCode == Enum.KeyCode.T then
        local track2 = Humanoid:LoadAnimation(Animations[math.random(#Animations)])
        particle.Enabled = true 
        particle1.Enabled = true
        particle2.Enabled = true 
        particle3.Enabled = true
        particle4.Enabled = true
        track:Play() 
        wait(0.35) 
        track:Stop()
        track2:Play()  --Animaton Follows--
        wait(0.45)
        track2:Stop()
        particle.Enabled = false
        particle1.Enabled = false
        particle2.Enabled = false
        particle3.Enabled = false
        particle4.Enabled = false
    end 
end)
0
if you call your randomNumber() function two times, there is only 20 percent chance that first will return 4, and 20 percent again that second returns 2. Miniller 562 — 5y
0
Thank you but instead I placed my definition above the if statement and it now woks perfectly. songboy50 77 — 5y
Ad
Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
5 years ago
Edited by Gey4Jesus69 5 years ago

Try using math.randomseed() to randomize the result. It takes a seed as the argument. You can input the current unix timestamp as the seed, as it will never repeat.

math.randomseed(tick())
local track2 = Humanoid:LoadAnimation(Animations[math.random(#Animations)])
0
I'm still recieving the same results. songboy50 77 — 5y

Answer this question