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

How do i make a Randomized Combat? [solved]

Asked by 6 years ago
Edited 6 years ago

I'm having trouble with the math.random() it keeps doing one thing only, And its not randomizing the combat at all. I cant find the error and i've looked through it 5 times now, Can anyone help?

player = game.Players.LocalPlayer.Character
cooldown = 1 --Put amount of seconds for cooldown to last.
db = false
Combat = math.random(1,2)

function onKeyPress(actionName, userInputState, inputObject)
    if userInputState == Enum.UserInputState.Begin then
        if db == false then 
        db = true
        --your code
        if Combat == 1 then
        print("Loading First Punch")
        local animation = Instance.new("Animation")
        animation.AnimationId = "http://www.roblox.com/Asset?ID=906039050"
        local animTrack = player.Humanoid:LoadAnimation(animation)
        animTrack:Play()
        wait(cooldown)
        db = false
        end
        if Combat == 2 then
        print("Loading Second Punch")
        local animation = Instance.new("Animation")
        animation.AnimationId = "http://www.roblox.com/Asset?ID=913285256"
        local animTrack = player.Humanoid:LoadAnimation(animation)
        animTrack:Play()
        wait(cooldown)
        db = false
        end
    end
    end
    end

game.ContextActionService:BindAction("keyPress", onKeyPress, false, "r")

****Thank you for the help :D

1 answer

Log in to vote
1
Answered by
DanzLua 2879 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

The reason the variable is staying the same is because you only set it once so it won't change, to fix this let's place where you set the variable inside the function so every time you click r it will change it

local player = game.Players.LocalPlayer.Character
local cooldown = 1 --Put amount of seconds for cooldown to last.
local db = false

function onKeyPress(actionName, userInputState, inputObject)
    local Combat = math.random(1,2)
    if userInputState == Enum.UserInputState.Begin then
        if db == false then 
        db = true
        --your code
        if Combat == 1 then
        print("Loading First Punch")
        local animation = Instance.new("Animation")
        animation.AnimationId = "http://www.roblox.com/Asset?ID=906039050"
        local animTrack = player.Humanoid:LoadAnimation(animation)
        animTrack:Play()
        wait(cooldown)
        db = false
        end
        if Combat == 2 then
        print("Loading Second Punch")
        local animation = Instance.new("Animation")
        animation.AnimationId = "http://www.roblox.com/Asset?ID=913285256"
        local animTrack = player.Humanoid:LoadAnimation(animation)
        animTrack:Play()
        wait(cooldown)
        db = false
        end
    end
    end
    end

game.ContextActionService:BindAction("keyPress", onKeyPress, false, "r")

Now Combat changes every time you click r

Ad

Answer this question