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

How to Remove Math Random Variable?

Asked by
yak602 10
4 years ago
Edited 4 years ago

I've been really confused with animations and sword scripts for quite a while. Recently I found this code:

local Damage = 10
local CanAttack = true
local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Character = Tool.Parent
local RaycastHitbox = require(Tool.RaycastHitbox)
local Hitbox = RaycastHitbox:Initialize(Tool, {Character})
Hitbox:PartMode(true)


local animations = {"4980024148","4980017811"} 
local CanAttack = true

script.Parent.Activated:connect(function()
if CanAttack == true then
script.Parent.CanDamage.Value = true
local animation = Instance.new("Animation",  script.Parent.Parent)
animation.AnimationId = "http://www.roblox.com/asset?id="..animations[math.random(1, #animations)]
local chosenanim = script.Parent.Parent.Humanoid:LoadAnimation(animation)
chosenanim:Play()
CanAttack = false
wait(1)
CanAttack = true
script.Parent.CanDamage.Value = false
end
end)

This code works perfectly however how would I remove the random variable? So animation 1 and 2 just play in sequence rather than random occurrence?

Example On click it Plays Animation 1 Animation 2 Animation 1 Animation 2

Instead of

Animation 1 Animation 1 Animation 1 Animation 2 etc just random

1 answer

Log in to vote
0
Answered by 4 years ago

Use a variable to index the table. Add one for each use, if it's 3, then it becomes 1.

Here's my example using a click detector.

local Part = Instance.new("Part",workspace);
local CD = Instance.new("ClickDetector",Part);

Part.Position = Vector3.new(0,10,0)

local Dinner_Plate = {
    'Carrot'; -- key/index 1
    'Turkey'; -- key/index 2
};

local NumberValue = 1

CD.MouseClick:Connect(function()
    NumberValue = NumberValue + 1
    if NumberValue>=3 then
        NumberValue = 1;
    end;
    print('Today, we will eat '..

        Dinner_Plate[NumberValue]

        );
end);

If you want, since there's only two values in that table, you can use a boolean which switches true to false and false to true with lua operator not.

local Part = Instance.new("Part",workspace);
local CD = Instance.new("ClickDetector",Part);

Part.Position = Vector3.new(0,10,0)

local Dinner_Plate = {
    'Carrot'; -- key/index 1
    'Turkey'; -- key/index 2
};

local BooleanValue = false

CD.MouseClick:Connect(function()
    BooleanValue = not BooleanValue
    print('Today, we will eat '..

        Dinner_Plate[(BooleanValue and 1)or 2]

        );
end);
Ad

Answer this question