#Need help with recoil-based knockback for my movement shooter game

1 messages · Page 1 of 1 (latest)

dry bear
#

I'm making a game where the movement relies on you shooting the opposite direction of where you want to propel yourself to. Right now, the problem i'm encountering is that after shoting, the pistol immediately drops down by gravity. i tried using massless but it seems like it's still affected by gravity.
(You may also give ideas for other parts of the script)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local shootEvent = ReplicatedStorage:FindFirstChild("ShootEvent")

local masslessDuration = 2 
local activeTimers = {} 

shootEvent.OnServerEvent:Connect(function(player)
    local character = player.Character
    if character then
        local pistol = character:FindFirstChild("PistolModel") or character.PrimaryPart
        if pistol then
            print("Pistol Found, Applying Coordinate-Based Recoil!")

            
            pistol.Massless = true

            
            pistol.CFrame = pistol.CFrame * CFrame.new(-15, 0, 0) 

           
            if activeTimers[player] then
                activeTimers[player]:Cancel()
            end

            
            activeTimers[player] = task.delay(masslessDuration, function()
                if pistol then
                    pistol.Massless = false 
                    activeTimers[player] = nil 
                    print("Gravity restored for", player.Name)
                end
            end)
        else
            warn("PistolModel NOT found in character!")
        end
    else
        warn("Character not found!")
    end
end)
zealous coral
dry bear
zealous coral
#

but I'd still keep it ticked regardless

dry bear
#

oh okay

zealous coral
#

no, it basically just makes that part negligible to whatever it's connected to

#

is the weld breaking or something?

#

could be that

dry bear
#

no, the weld seems to still be intact

#

do you want a video demo?

zealous coral
#

yeah

#

also why're you setting the CFrame to -15 studs in the x?

dry bear
#

you can see how clunky the movement feels because of the gravity

zealous coral
#

ohhhh okay I see

dry bear
#

which i dont like

ruby crowBOT
#

studio** You are now Level 7! **studio

zealous coral
#

I didn't think about the faces on the mesh or model being set up like that

#

you'll just have to anchor it

dry bear
#

oh yeah, that could work

#

although i feel like i'm ignoring a potential issue with the pistol clipping through walls because of the cframe..

zealous coral
#

That’s easily fixable

#

Just raycast where you intend to go

#

And if it hits anything, adjust accordingly

dry bear
#

ok, i got an error. it's related to task.delay, but since that part is what manage the time for when the pistol get unanchored, it makes the pistol unachored earlier than anticipated

#

probably because the code skipped it

#

updated version

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local shootEvent = ReplicatedStorage:FindFirstChild("ShootEvent")

local masslessDuration = 2 
local activeTimers = {} 

shootEvent.OnServerEvent:Connect(function(player)
    local character = player.Character
    if character then
        local pistol = character:FindFirstChild("PistolModel") or character.PrimaryPart
        if pistol then
            print("Pistol Found, Applying Coordinate-Based Recoil!")

            
            pistol.Massless = true
            pistol.Anchored = true

            
            pistol.CFrame = pistol.CFrame * CFrame.new(-15, 0, 0) 

           
            if activeTimers[player] then
                activeTimers[player]:Cancel()
            end

            
            activeTimers[player] = task.delay(masslessDuration, function()
                if pistol then
                    pistol.Massless = false 
                    pistol.Anchored = false
                    activeTimers[player] = nil 
                    print("Gravity restored for", player.Name)
                end
            end)
        else
            warn("PistolModel NOT found in character!")
        end
    else
        warn("Character not found!")
    end
end)
#

oh yeah, here's the error
12:13:50.680 ServerScriptService.Script:23: attempt to index thread with 'Cancel' - Server - Script:23

#

the idea for that part was that i still want the pistol to fall after a period of time, so i made it wait a period of time. each shot is meant to reset the time

zealous coral
dry bear
#

i still want the pistol to drops to the ground when the pistol isnt shooting

zealous coral
dry bear
#

do you mean bodyvelocity?

#

already tried that