#script stops working after death

1 messages · Page 1 of 1 (latest)

worthy saffron
#

so basically what im trying to make is a viewmodel, local first person arms, like in fps games. what i did is i cloned a rig from replicated storage and then i synced it to the camera, and i tried to sync the animations, but there is one problem. here is the script (i know its messy, sorry):

#
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera

Player.CharacterAdded:Connect(function(character)

    local viewmodel = game.ReplicatedStorage.ViewModel:Clone()
    local RemoteEvent = game.ReplicatedStorage:WaitForChild("UpdateHeadAndArms")
    local humanoid = character:WaitForChild("Humanoid"):WaitForChild("Animator")

    local physicsService = game:GetService("PhysicsService")
    local hum = viewmodel:WaitForChild("Humanoid")

    viewmodel.Parent = Camera
    viewmodel.PrimaryPart = viewmodel:WaitForChild("HumanoidRootPart")

    local idle = hum:LoadAnimation(script.Animation)

    if Player.Team == game.Teams.Red then
        for _, part in pairs(viewmodel:GetDescendants()) do
            if part:IsA("BasePart") then
                part.CollisionGroup = "RedTeam"
            end
        end
    else 
        for _, part in pairs(viewmodel:GetDescendants()) do
            if part:IsA("BasePart") then
                part.CollisionGroup = "BlueTeam"
            end
        end
    end

    for _, part in character:GetDescendants() do
        if part:IsA("BasePart") then
            part.CastShadow = true
        end
    end

    idle:Play()

    RunService.RenderStepped:Connect(function()
        local cameraLookVector = Camera.CFrame.LookVector
        viewmodel:PivotTo(
            Camera.CFrame * CFrame.new(0, -1, -1)
        )
        if RemoteEvent then
            RemoteEvent:FireServer(cameraLookVector)
        end
    end)

    local tracks = {}

    character.animTool.OnClientEvent:Connect(function(anim, play)

        if play then
            local track = hum:LoadAnimation(anim)
            tracks[anim] = track
            track:Play()
        else
            if tracks[anim] then
                tracks[anim]:Stop()
                tracks[anim] = nil
            end
        end
    end)
#
        if tool then

            local othertool = viewmodel:FindFirstChildOfClass("Tool")
            local clone = tool:Clone()
            clone.Parent = viewmodel
            clone.Handle.CFrame = CFrame.new()

            if othertool then
                othertool:Destroy()
            end

            if clone:FindFirstChild("IsMotored") then
                local other = viewmodel.Torso:FindFirstChild("Motor6D")
                if other then
                    other:Destroy()
                end

                local motor = Instance.new("Motor6D", viewmodel.Torso)
                motor.Part0 = viewmodel["Right Arm"]
                motor.Part1 = clone.Handle

            else
                local other = viewmodel.Torso:FindFirstChild("Weld")
                if other then
                    other:Destroy()
                end

                local weld = Instance.new("Weld", viewmodel.Torso)
                weld.Part0 = viewmodel["Right Arm"]
                weld.Part1 = clone.Handle
            end
        end
    end)

    character:WaitForChild("Humanoid").Died:Connect(function()
        viewmodel:Destroy()
        return
    end)

end)

so it basically works, until you die once, ut it doesnt totally break, its just that the animations cant stop

thick cave
#

where is this script located

worthy saffron
#

starter player

thick cave
#

try to put it on startercharacter

#

starterplayer only runs until death

worthy saffron
#

i mean, the script still runs, like everything is working perfectly expect the animations wont stop, thats so weird and specific

#

but ill test that

#

that did not work

cursive zincBOT
#

studio** You are now Level 1! **studio

thick cave
#

hmm

#

bring it back to starterplayer scripts then

#

let me read the code

worthy saffron
#

i can provide more context and scripts if you want

thick cave
#

are there any errors on the output when you die?

worthy saffron
#

not really

thick cave
#

only the animations break?

worthy saffron
#

no, so basically everything that you would expect to happen, happens. the animtions play and everything but it just doesnt stop

#

i can send a ss of the script so its less complicated

#

    character.animTool.OnClientEvent:Connect(function(anim, play)

        if play then
            local track = hum:LoadAnimation(anim)
            tracks[anim] = track
            track:Play()
        else
            if tracks[anim] then
                tracks[anim]:Stop()
                tracks[anim] = nil
            end
        end
    end)```

this is the part that plays the animations
thick cave
#

i cant send the code because im too lazy rn

#

but what i think is happening

#

you said that the animations cant stop right

worthy saffron
#

yeah

thick cave
#

Characteradded fires everytime the character is added

#

what this means?

#

tracks is being created multiple times

#

and probably when you die once, it is trying to find the animation on the other tracks table

#

on the second life table if you get what i mean

worthy saffron
#

i dont get what this means right now but i just gotta think about it a little

#

im new to scripting

thick cave
worthy saffron
#

do i need to reset it after the character dies

#

?

thick cave
#

was about to say that

worthy saffron
#

alr thanks bro upvote

thick cave
#

not on the Died event

worthy saffron
#

the past thing didnt work let me try that

#

that didnt work either

thick cave
#

how are you cleaning the table

worthy saffron
#

i just did table = {}

#

or should i do nil

thick cave
#

nah u did right

#

nil removes the table

#

im trying to see what is wrong

worthy saffron
#

btw when you fire animTool you are inputting player, animation object, then true or false depending on whether you want it to play or stop

cursive zincBOT
#

studio** You are now Level 2! **studio

thick cave
#

what this one remote do

worthy saffron
#

i just answered that lol

thick cave
#

timing lol

#

my message was still sending

worthy saffron
#

those are fired from tools

#

i put a print() inside the logic part where it stops the animation and it does not print anything

worthy saffron
#

yea

#

after the first life

thick cave
worthy saffron
#

what is it

thick cave
#

can you try to print the tracks table inside the onclientevent

worthy saffron
#
    character.animTool.OnClientEvent:Connect(function(anim, play)
print(table)
        
        if play then
            local track = hum:LoadAnimation(anim)
            tracks[anim] = track
            track:Play()
        else
            
            tracks[anim]:Stop()
            tracks[anim] = nil
            
        end
    end)

you mean like this?

thick cave
#

yea

worthy saffron
#

it prints some nonsense like this

00:49:49.585 ▼ {
["clear"] = "function",
["clone"] = "function",
["concat"] = "function",
["create"] = "function",
["find"] = "function",
["foreach"] = "function",
["foreachi"] = "function",
["freeze"] = "function",
["getn"] = "function",
["insert"] = "function",
["isfrozen"] = "function",
["maxn"] = "function",
["move"] = "function",
["pack"] = "function",
["remove"] = "function",
["sort"] = "function",
["unpack"] = "function"

it works in after death too but idk what this even means

thick cave
#

huh

#

why is it printing table functions

#

thats crazy

#

well idk realy

#

what i think is that the script is not stopping the right animation

#

uhh

#

could you try

#
character.animTool.OnClientEvent:Connect(function(anim, play)
        local id = anim.AnimationId
        
        if play then
            local track = hum:LoadAnimation(anim)
            tracks[id] = track
            track:Play()
        else
            if tracks[id] then
                tracks[id]:Stop()
                tracks[id] = nil
            end
        end
    end)
#

changing the current animation part to that

#

?

worthy saffron
#

alright

#

do i need to delete the
local track = {}
from before

thick cave
#

nah

worthy saffron
#

i mean local table

thick cave
#

is there any difference?

worthy saffron
#

if not this didnt work either, im really wasting your time sorry but if youre still interested maybe i can send an example code from a tool:

script.Parent.Activated:Connect(function()
if turn then
turn = false
slash1:Play()
animtool:FireClient(player, script.Parent.Slash1, true)
cool(tool.Cooldown.Value)
task.delay(2, function()
turn = true
end)
else
turn = true
slash2:Play()
animtool:FireClient(player, script.Parent.Slash2, true)
cool(tool.Cooldown.Value)

    end
    
end)
thick cave
worthy saffron
#

me too bro its like why would it start playing if its not gonna stop

thick cave
#

maybe u gotta wait someone with more experience to guide you

#

i cant rlly see any errors on it

#

mybe it is a little thing

worthy saffron
#

alr bro thanks for the efforts and time tho

thick cave
#

you said

#

play is true

#

or false

#

right

worthy saffron
#

yep

thick cave
#

you are sending true on both fires on the tool script

#

you never fired false

worthy saffron
#

i probably did and didnt send you that part because i didnt want you to read stuff that arent related

#

i know i did because before you die you can stop the animations

thick cave
#

i just realised

#

you needed to print(tracks)

#

not print(table)

#

damn why didnt i see that

worthy saffron
#

right

#

my bad

#

01:03:49.418 ▼ {
[Instance(Idle,CB4A697C3B9D7A37)] = Idle,
[Instance(Slash1,18B0BCDB3C205D57)] = Slash1,
[Instance(Slash2,E4EEE1D290A63D47)] = Slash2
} - Client - Arms:55

and then

Players.sigmaironingtable.PlayerScripts.Arms:63: attempt to index nil with 'Stop' - Client - Arms:63

arms is the name of the script btw

cursive zincBOT
#

studio** You are now Level 3! **studio

thick cave
#

are you using the code i sent?

#

`

#

    character.animTool.OnClientEvent:Connect(function(anim, play)
        local id = anim.AnimationId
        
        if play then
            local track = hum:LoadAnimation(anim)
            tracks[id] = track
            track:Play()
        else
            if tracks[id] then
                tracks[id]:Stop()
                tracks[id] = nil
            end
        end
    end)
#

this one

#

but still why is it saying that tracks is nil

worthy saffron
#

01:07:17.173 ▼ {
["rbxassetid://103372683553799"] = Slash1,
["rbxassetid://137015450951644"] = Idle,
["rbxassetid://81446549403565"] = shoot
} - Client - Arms:56

thick cave
#

not tracks i mean the animation

thick cave
worthy saffron
#

tried to index nil with stop? no

thick cave
#

you never send a false signal

#

what is cool?

worthy saffron
#

hold on

#
local animtool = nil
local tool = script.Parent
local hitbox = tool.Hitbox
local character = nil
local player = nil
local touched = false
script.Parent.Equipped:Connect(function()
    local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
    local slash1 = humanoid:LoadAnimation(script.Parent.Slash1)
    local slash2 = humanoid:LoadAnimation(script.Parent.Slash2)
    local idle = humanoid:LoadAnimation(script.Parent.Idle)
    character = script.Parent.Parent
    player = game.Players:GetPlayerFromCharacter(character)
    local turn = true
    local other = character.Torso:FindFirstChild("Motor6D")
    if other then
        other:Destroy()
    end
    animtool = character:FindFirstChild("animTool")
    
    idle:Play()
    animtool:FireClient(player, script.Parent.Idle, true)
    character:FindFirstChild("Right Arm"):WaitForChild("RightGrip"):Destroy()
    local motor6 = Instance.new("Motor6D", character.Torso)
    motor6.Part0 = character:FindFirstChild("Right Arm")
    motor6.Part1 = script.Parent.Handle
    script.Parent.Unequipped:Connect(function()
        idle:Stop()
        animtool:FireClient(player, script.Parent.Idle, false)
    end)
    local function cool(time)
        tool.Enabled = false
        task.wait(time)
        tool.Enabled = true
        touched = false
    end
#
    script.Parent.Activated:Connect(function()
        if turn then
            turn = false
            slash1:Play()
            animtool:FireClient(player, script.Parent.Slash1, true)
            cool(tool.Cooldown.Value)
            task.delay(2, function()
                turn = true
            end)
        else
            turn = true
            slash2:Play()
            animtool:FireClient(player, script.Parent.Slash2, true)
            cool(tool.Cooldown.Value)

        end
        
    end)
    

end)

hitbox.Touched:Connect(function(hit)
    if hit:IsDescendantOf(character) then return end
    if tool.Enabled == true then return end
    if touched then return end
    touched = true
    
    local hmn = hit.Parent:FindFirstChild("Humanoid")
    if hmn then
        hmn:TakeDamage(tool.Damage.Value)
    end
    
end)
#

thats the full script of the tool

#

cool is a function for cooldowns

#

it disables and enables the tool

thick cave
#

well

#

there is alot of things there

#

first mistake is that you are recreating the variable turn everytime that you equip the tool

#

second one is that you are creating a activated event inside a equipped event

#

this will duplicate the activated event everytime you equip the tool

#

and yeah you are never sending a false signal to stop the animation

#

i think what you can do is firing it again after the cooldown but now with false

#

i think this is the time when AI would rlly be useful now

#

AI will probably explain way better htan me

#

than me*

#

dont copy the code from it oblivously, try to understand

worthy saffron
#

i already asked ai like a few times and didnt understand 😭

#

bro im gonna have to go now thanks for the effort man

#

ill try what you said tho

thick cave
worthy saffron
#

thanks again

thick cave
#

there is alot of begginer mistaskes there but for sure you will fix them

#

but yea dont be mad to make these mistakes because failing is a great way to evolve

devout veldt
#

ok so

#

i tryed to read everything

#

but its hard

#

right here

#

wait

#

i dont think that is it

#

ok so

#

first question

#

which animations wont stop

#

and in what way

#

its helpfull if u have a vid

worthy saffron
#

action type animations will stop automatically, youre only required to stop idle animations

#

ill try to get it on video but my pc isnt that great

devout veldt
#

second question is what does the tool script do?

#

exactly

worthy saffron
#

its just from an example tool i dont think its really related to the actual script. but all you do from a tool script is you fire a remote event inside the character called animTool

animTool:FireClient(player, animation object, bool)

if you input an animation and true on the server side, it should animate the rig on the local side. if you input false, it should stop the animation

devout veldt
#

ok

#

so

#

i think i know what is happening

#

on the first script that u sent

#

u are using characteradded

#

actually wait a little

#

le me confirm

#

ye

#

that is weird

#

even the things that i think are the problem actually are not

#

i would think that it is bc the script is creating a new connection every time your character loads

#

but i dont think that is it

worthy saffron
#

im tryna fix that right now i didnt know connections worked like that

#

until now

#

im kinda new

devout veldt
#

ye

#

connections still run even after your character dies

#

if u want do undo the connection u would need to do sum like

#

local con = blabla:Connect(blablabla)

(when u want to disconnect)

con:Disconnect()

cursive zincBOT
#

studio** You are now Level 12! **studio

devout veldt
#

u can also disconnect inside the function of the connect

#

kinda like a return

#

but for connections

devout veldt
#

i thought u could but u cant

#

soo uhh

#

debugging with prints time

devout veldt
#

can u put a print of the tracks, anim and the play

#

ping me when u do

worthy saffron
#

sorry i was doing something i just came back

#

it print this after i print tracks, anim, play

{
[Instance(Idle,E506FB849A4A2D3E)] = Idle,
[Instance(Slash1,4794CFFF69CB4FE)] = Slash1
} Slash1 true

devout veldt
#

just that?

#

anything else?

#

can i have a vid?

#

i feel like im missing something

worthy saffron
#

when i unequip after dying once it prints this

{[Instance(Slash1,FE9D41C7A543723)] = Slash1
} Idle false

it looks pretty normal and it should stop the animation but idk whats happening

#

i was gonna record in roblox but it doesnt even work in the actual game 🙏

devout veldt
#

u can record on studio

#

f12

worthy saffron
#

do you know where to find the records

#

like the files

devout veldt
#

videos

#

roblox

worthy saffron
devout veldt
#

hmm

#

ok so

#

lets just try some basic things

#

i saw that u are placing the animations inside the character

#

put them on replicated storage and load them from there

#

u can make a folder if u want

worthy saffron
#

the animations are inside the tool

cursive zincBOT
#

studio** You are now Level 4! **studio

worthy saffron
#

but ok

#

that didnt work

#

im gonna try to disconnect the functions first to see if that works

devout veldt
#

ok

#

im looking at the scripts on the studio to see if that helps me

devout veldt
worthy saffron
#

do you want me to share the game with you temporarily? i can do that

devout veldt
#

okk

spare spear
#

reminder that literally any time something stops working when the character is reset USUALLY HAS SOMETHING TO DO WITH THE CHARACTER

cursive zincBOT
#

studio** You are now Level 35! **studio

spare spear
#

seen this problem a FEW times in the past two days

#

and I don't mean literally just a few.

#

ts is so solvable if all character/animation/humanoid/whatnot data is maintained correctly WITH the character state rather than remotely.

#

I strongly recommend making this more built on states.

worthy saffron
spare spear
#

character scripts at the very least