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

How do i make a Randomized Combat? [solved]

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

✓ Accepted answer

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

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