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

Why does my Module Script not work properly after you die? [Solved myself]

Ok so hello I have a simple-looking yet kind of not-understandable problem. I have a weapon which has a local script, which relies almost fully on a module script, to make things much easier to do. It does work, straight after your character is loaded into the map via player:LoadCharacter which is in another script but that is just extra information. However, after you die, no events seem to connect to anything. The stuff is loaded correctly, and there are no errors in the output (this is why it is hard). For example, when I activate the weapon, nothing connects, which is weird, considering it worked first time.

Recent note: Some kind of useless stuff left in recent code yes, as I attempted to fix the error in the module, but it didn't work. I kind of found out that the only functions that do work are the set function, called from outside, and the character added function, even after you die. But the functions that connect themselves inside with the tool do not work. Anybody got any idea? Module scripts do not run twice or more, so.... But I don't want to make stuff connect outside because I want to keep the local script simple.

Local script:

--Very simple and efficient yes
print("Local start")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
	 
local moduleScripts = ReplicatedStorage:WaitForChild("ModuleScripts")
local MeleeAnimModuleScript = require(moduleScripts:WaitForChild("MeleeAnimModuleScript"))
MeleeAnimModuleScript.set(5172270754, 5174484810)
print("Set")

My Module Script:

local MeleeAnimModuleScript = {}
--Services
local StarterPack = game:GetService("StarterPack")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--Variables
local localPlayer = Players.LocalPlayer

local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
print(character)
local humanoid = character:WaitForChild("Humanoid")
local localTool

--Ummmm some thing to make sure humanoid is correct
localPlayer.CharacterAdded:Connect(function(newCharacter)        
	character = newCharacter
	humanoid = character:WaitForChild("Humanoid")
	localTool = localPlayer.Backpack:WaitForChild("Baton") or character:WaitForChild("Baton")
	print("Set")
	print(localTool:GetDescendants())
end)

repeat
	wait()
until humanoid:IsDescendantOf(game)and localTool ~= nil
print(localPlayer)

local hitAnimRunning = false
local idleAnimRunning = false
local canStart = true

local hitStart = StarterPack:WaitForChild("HitStart")
local hitEnd = StarterPack:WaitForChild("HitEnd")
local idleStart = StarterPack:WaitForChild("IdleStart")

local hitAnimTrack
local idleAnimTrack
local set = false
--Function to load animations
local function loadAnim(number)
	repeat
		wait()
	until humanoid:IsDescendantOf(game)
	local anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://"..tostring(number)
	local track = humanoid:LoadAnimation(anim)
	print("Loaded")
	return(track)
end

--This is an important function, making the variables.
function MeleeAnimModuleScript.set(hit, idle)
	hitAnimTrack = loadAnim(tonumber(hit))
	idleAnimTrack = loadAnim(tonumber(idle))
	set = true
	print(localTool.Parent)
end

--Simple module function that can be used in local scripts for tools, if there is correct setup
local function meleeAnim()
	print("Tried")
	if hitAnimRunning == false and set == true then
		hitAnimRunning = true 
		hitAnimTrack:Play()
		--Fires event to server when swingdown happens
		hitAnimTrack:GetMarkerReachedSignal("HitStart"):Connect(function()
			if canStart ~= true then return end
			canStart = false
			hitStart:FireServer()
			hitAnimTrack:GetMarkerReachedSignal("HitEnd"):Connect(function()
				hitEnd:FireServer()
				hitAnimTrack:GetMarkerReachedSignal("WholeSwingEnd"):Connect(function()
					--Let the animation run a bit more so it isn't instant when player clicks again
					wait(0.5)
					hitAnimRunning = false
					canStart = true
				end)
			end)
		end)
	end
end

local function stopAnim()
	if set == false then return end
	hitAnimTrack:Stop()
	idleAnimTrack:Stop()
	hitAnimRunning = false
	idleAnimRunning = false
end

localTool.Equipped:Connect(function()
	print("Equipped")
	repeat
		wait()
	until set == true
	idleAnimRunning = true
	if idleAnimRunning == true then
		idleAnimTrack:Play()
		repeat
			wait()
		until idleAnimRunning == false
	end
end)

localTool.Unequipped:Connect(function()
	print("Unequipped")
	stopAnim()
end)

localTool.Activated:Connect(meleeAnim)

humanoid.Died:Connect(function()
	print("Died")
	localTool.Parent = ReplicatedStorage
	set = false
end)

print("Finished")
return MeleeAnimModuleScript

I am kind of confused with this so thank you if you work out this, since the code runs through correctly. Thanks in advance. (Changed module a bit still does not work)

2 answers

The best way would be to put your script in StarterCharacterScripts to make sure script reloads everytime a character dies. If you do that, you can remove this section too, as it will be no longer necessary:

--Ummmm some thing to make sure humanoid is correct
localPlayer.CharacterAdded:Connect(function(newCharacter)
    local refresh = coroutine.wrap(function()
        print("New")
        character = newCharacter
        humanoid = character:WaitForChild("Humanoid")
        print("Set")
    end)
    refresh()
end)

Another benefit of StarterCharacterScripts is that you can replace this:

local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()

With this:

local character = script.Parent

If for any reason you cannot move your scripts, you need to reload the module manually. Add this connection to your main script:

--Very simple and efficient yes
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local moduleScripts = ReplicatedStorage:WaitForChild("ModuleScripts")
local MeleeAnimModuleScript = require(moduleScripts:WaitForChild("MeleeAnimModuleScript"))

local player = game:GetService("Players").LocalPlayer
player.CharacterAdded:Connect(function(character)
	local hum = character:WaitForChild("Humanoid")
	hum:WaitForChild("Animator")
	MeleeAnimModuleScript.set(5172270754, 5174484810)
end)

Hope this helps!

2 comments

0

I did not really use the second option since the local script is in a tool, so it re-runs every time as a new thing, so that was ok. But idk why there is an error saying that there is infinite yield possible when waiting for the tool, on the first option, even though it literally says Phyrixia.Backpack in the output. It should work but there is a problem with the definition of the tool.

0

Ty for suggestion though.

I fixed it myself people. Put most things into set func to make connections correct lol.

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