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

Throwing issues on my Murder Mystery game, please help?

Asked by 2 years ago

Hello! I am currently making a murder mystery game on Roblox and I am having issues with the throwing. Simply when I press E (the throw knife button) the throwing animation plays on every other part except the arm with the knife and the knife doesn't release or throw it just stays in the hand. I don't know what I am doing wrong, and that is why I am here. Please help! Code listed below:

-- Basic settings
local ALLOWED_DISTANCE = 8
local XP_PER_KILL = script.Parent.KillXP.Value

local tool = script.Parent
local debounce = false
local throwCooldown = false
local allowedToHit = false
local throwType = 0
local lastMouseHitPos = nil

local toolPlr
local animDraw = nil
local animStab = { nil, nil, nil }
local animThrow = nil

function enableEffects()

    -- Enable em
    for _, i in pairs(tool.Handle:GetChildren()) do
        if i.Name == "Effect" then
            i.Enabled = true
        end
    end

end

function disableEffects()

    -- Disable em
    for _, i in pairs(tool.Handle:GetChildren()) do
        if i.Name == "Effect" then
            i.Enabled = false
            i:Clear()
        end
    end

end

script.HitPlayer.OnServerEvent:connect(function(plr, hit)

    -- Make sure the hit is valid
    if hit == nil or hit.Parent == nil then
        return
    end

    -- Check if player (and not this player)
    if hit.Parent:FindFirstChild("Humanoid") and allowedToHit and plr == toolPlr then
        local chr = hit.Parent
        if chr ~= tool.Parent and chr.Humanoid.Health > 0 then

            -- Check if not on same team
            local okay = true
            local otherPlr = game.Players:GetPlayerFromCharacter(chr)
            if chr:FindFirstChild("Knife") then
                if chr.Knife.TeamColor.Value == tool.TeamColor.Value then
                    okay = false
                end
            end
            if chr:FindFirstChild("Revolver") then
                if chr.Revolver.TeamColor.Value == tool.TeamColor.Value then
                    okay = false
                end
            end
            if otherPlr.Backpack:FindFirstChild("Knife") then
                if otherPlr.Backpack.Knife.TeamColor.Value == tool.TeamColor.Value then
                    okay = false
                end
            end
            if otherPlr.Backpack:FindFirstChild("Revolver") then
                if otherPlr.Backpack.Revolver.TeamColor.Value == tool.TeamColor.Value then
                    okay = false
                end
            end

            -- Tell server to hit
            if okay and math.abs((hit.Position - tool.Handle.Position).Magnitude) <= ALLOWED_DISTANCE then

                -- Kill the player
                local chr = hit.Parent
                chr.Humanoid.Health = 0
                game.ServerScriptService.Functions.GameDeath:Fire(chr, (chr.Torso.Position - tool.Parent.Torso.Position).Unit, tool.Effect.Value)

                -- Award XP
                if XP_PER_KILL > 0 then
                    local plr = game.Players:GetPlayerFromCharacter(tool.Parent)
                    game.ServerScriptService.Functions.GiveXP:Fire(plr, XP_PER_KILL, 0)
                end
                plr.Achievements.StabKills.Value = plr.Achievements.StabKills.Value + 1

                -- Play hit sound and effect
                game.ReplicatedStorage.Interactions.Client.PlaySound:FireAllClients(tool.Handle.Hit)
                game.ReplicatedStorage.Interactions.Client.RunKillFlash:FireClient(toolPlr)

            end

        end
    end

end)

script.ClientStab.OnServerEvent:connect(function(plr)

    -- Make sure correct player
    if plr ~= toolPlr then
        return
    end

    -- See if should
    if not debounce then
        debounce = true

        -- Allow hitting other players      
        allowedToHit = true

        -- Play the stab animation
        animStab[math.random(1, #animStab)]:Play()
        wait(0.2)
        game.ReplicatedStorage.Interactions.Client.PlaySound:FireAllClients(tool.Handle.Swing)

    end

end)

script.ClientThrow.OnServerEvent:connect(function(plr)

    -- Make sure correct player
    if plr ~= toolPlr then
        return
    end

    -- See if should
    if not debounce and not throwCooldown then
        debounce = true

        -- Play the throw animation
        animThrow:Play()
        throwType = 0

    end

end)

script.ClientThrowStart.OnServerEvent:connect(function(plr)

    -- Make sure correct player
    if plr ~= toolPlr then
        return
    end

    -- See if should
    if not debounce and not throwCooldown and not animThrow.IsPlaying then
        debounce = true

        -- Play the throw animation
        animThrow:Play()
        throwType = 1

    end

end)

script.ClientThrowEnd.OnServerEvent:connect(function(plr, hitPos)

    -- Make sure correct player
    if plr ~= toolPlr then
        return
    end

    -- See if should
    if animThrow.IsPlaying and animThrow.Speed == 0 then

        -- Finish the throw animation
        lastMouseHitPos = hitPos
        animThrow:AdjustSpeed(1)

    end

end)

tool.Equipped:connect(function()

    -- Set player
    toolPlr = game.Players:GetPlayerFromCharacter(tool.Parent)

    -- Load anim draw
    animDraw = tool.Parent.Humanoid:LoadAnimation(script.KnifeDraw)
    animDraw.Stopped:connect(function()
        tool.Handle.Transparency = 0
        debounce = false
    end)

    -- Load anim stabs
    animStab[1] = tool.Parent.Humanoid:LoadAnimation(script.KnifeStab1)
    animStab[1].Stopped:connect(function()
        allowedToHit = false
        debounce = false
        tool.Handle.Transparency = 0
    end)
    animStab[2] = tool.Parent.Humanoid:LoadAnimation(script.KnifeStab2)
    animStab[2].KeyframeReached:connect(function(name)
        if name == "Beginning" then
            tool.Grip = tool.NormalGrip.Value * CFrame.Angles(0, 0, math.pi)
        end
    end)
    animStab[2].Stopped:connect(function()
        tool.Grip = tool.NormalGrip.Value
        allowedToHit = false
        debounce = false
        tool.Handle.Transparency = 0
    end)
    animStab[3] = tool.Parent.Humanoid:LoadAnimation(script.KnifeStab3)
    animStab[3].Stopped:connect(function()
        allowedToHit = false
        debounce = false
        tool.Handle.Transparency = 0
    end)

    -- Load anim throw
    animThrow = tool.Parent.Humanoid:LoadAnimation(script.KnifeThrow)
    animThrow.KeyframeReached:connect(function(name)
        if name == "Peak" and tool.Parent.ClassName ~= "Backpack" and throwType == 1 then

            -- Pause the animation
            animThrow:AdjustSpeed(0)

        elseif name == "Throw" and tool.Parent.ClassName ~= "Backpack" then

            -- Set up the knife projectile
            local copy = tool.Handle:clone()
            copy.Name = "KnifeProjectile"
            copy.Transparency = 0
            copy.Anchored = true
            local scr = script.KnifeProjectileScript:clone()
            scr.Owner.Value = tool.Parent
            scr.TeamColor.Value = tool.TeamColor.Value
            local hitPos = nil
            if throwType == 0 then
                hitPos = script.ServerGetMouseHitPosition:InvokeClient(toolPlr)
            elseif throwType == 1 then
                hitPos = lastMouseHitPos
            end
            scr.Velocity.Value = (hitPos - copy.Position).Unit * script.KnifeThrowSpeed.Value
            scr.Effect.Value = tool.Effect.Value
            scr.RotateAxis.Value = tool.RotateAxis.Value
            scr.GripCFrame.Value = (tool.NormalGrip.Value - tool.NormalGrip.Value.p):inverse()
            scr.Parent = copy

            -- Place in workspace
            copy.Parent = workspace
            scr.Disabled = false

            -- Make normal knife invisible
            tool.Handle.Transparency = 1
            disableEffects()

            -- Activate cooldown
            throwCooldown = true
            spawn(function()
                wait(0.5)
                tool.Handle.Transparency = 0
                debounce = false
                wait(script.KnifeThrowCooldown.Value - 0.5)
                throwCooldown = false
            end)

        end
    end)
    animThrow.Stopped:connect(function()
        if tool.Parent.ClassName ~= "Backpack" and tool.Handle.Transparency >= 1 then
            tool.Handle.Transparency = 0
            animDraw:Play()
            enableEffects()
            game.ReplicatedStorage.Interactions.Client.PlaySound:FireAllClients(tool.Handle.Draw)
        else
            tool.Handle.Transparency = 0
            enableEffects()
            debounce = false
        end
    end)

    -- Draw the knife
    debounce = false
    animDraw:Play()
    enableEffects()
    game.ReplicatedStorage.Interactions.Client.PlaySound:FireAllClients(tool.Handle.Draw)
    tool.Handle.Transparency = 0

    -- Make display knife invisible
    if tool.Parent:FindFirstChild("DisplayKnife") then
        tool.Parent.DisplayKnife.Transparency = 1
        for _, i in pairs(tool.Parent.DisplayKnife:GetChildren()) do
            if i.ClassName ~= "SpecialMesh" and i.ClassName ~= "Weld" then
                i.Enabled = false
            end
        end
    end

end)

tool.Unequipped:connect(function()

    -- Destroy animation tracks
    animThrow:Stop()
    animThrow:destroy()
    animDraw:Stop()
    animDraw:destroy()
    for i=1, 3 do
        animStab[i]:Stop()
        animStab[i]:destroy()
    end

    -- Make display knife visible
    local chr = tool.Parent
    if tool.Parent ~= nil and tool.Parent.ClassName == "Backpack" then
        chr = tool.Parent.Parent.Character
    end
    if chr ~= nil then
        if chr:FindFirstChild("DisplayKnife") then
            chr.DisplayKnife.Transparency = 0
            for _, i in pairs(chr.DisplayKnife:GetChildren()) do
                if i.ClassName ~= "SpecialMesh" and i.ClassName ~= "Weld" then
                    i.Enabled = true
                end
            end
        end
    end

end)

1 answer

Log in to vote
0
Answered by
enes223 327 Moderation Voter
2 years ago

hey you! have you ever heard of enes? if you are in trouble, better call enes!

0
not helping tho T3_MasterGamer 2189 — 2y
Ad

Answer this question