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

How do I make punching do random damage?

Asked by 5 years ago

This is the punch script in ServerScriptService:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage)
punchEvent.Name = "PunchEvent"

local animations = {2837975148, 2837983710}
local function onPunchFired(plr)
    local char = game.Workspace:FindFirstChild(plr.Name)
    local humanoid = char.Humanoid
    local animation = Instance.new("Animation")
    local picked = math.random(1, #animations)
    animation.AnimationId = "http://roblox.com/asset/?id="..animations[picked]
    local animTrack = humanoid:LoadAnimation(animation)
    animTrack:Play()
    local dmgScript = script.DmgScript:Clone()
    if picked == 1 then
        dmgScript.Parent = char.RightHand
    elseif picked == 2 then
        dmgScript.Parent = char.LeftHand
    end
    dmgScript.Disabled = false
    wait(1.4)
    dmgScript:Destroy()
end

punchEvent.OnServerEvent:Connect(onPunchFired)

Then this is the damage script inside of the punch script:

script.Parent.Touched:Connect(function(hit)
    local char = hit.Parent
    local hum = char:FindFirstChild("Humanoid")
    if hum and char.Name ~= script.Parent.Parent.Name then
        hum.Health = hum.Health - 10
        script.Disabled = true
        wait(1.4)
        script.Disabled = false
    end
end)


Here is the script to tell which key to hit to punch:

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = ReplicatedStorage:WaitForChild("PunchEvent")

local ready = true

local function punch(inputObject, gameProcessed)
    if inputObject.KeyCode == Enum.KeyCode.Q and ready then
        punchEvent:FireServer()
        ready = false
        wait(0.5)
        ready = true
    end
end

UserInputService.InputBegan:Connect(punch)
0
math.random(0,10) -- picks a random number 0-10 wookey12 174 — 5y
0
Thanks ChefDevRBLX 90 — 5y

1 answer

Log in to vote
1
Answered by
starmaq 1290 Moderation Voter
5 years ago

Well this is incredibly easy, as wookey said, you got use math.random. this is really helpful, it will generate a number between the 2 given arguments giveb which are obviously a number valule. If you put in 0, 10 it will generate a random number betwenn 0 and 10, it can be 6, 5, 8, 4, 10.. Now if you put in 1 argument it will generate a number between that given number and 0. math.random(5) will return either 1, 4, 3..

Now I think you got the idea! All you gotta do in your script is

script.Parent.Touched:Connect(function(hit)
    local char = hit.Parent
    local hum = char:FindFirstChild("Humanoid")
    if hum and char.Name ~= script.Parent.Parent.Name then
        hum.Health = hum.Health - math.random(0, 10)
        script.Disabled = true
        wait(1.4)
        script.Disabled = false
    end
end)

and that's it! you may wanna also check math.randomseed() which is also related to this! Here!

0
Thank you that was easy. ChefDevRBLX 90 — 5y
0
np! starmaq 1290 — 5y
Ad

Answer this question