ModuleScript not being updated on death

When my character dies, a modulescript doesn’t update to the new character.
StarterCharacterScripts (so this SHOULD fire on every new character, correct?)

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local Modules = ReplicatedStorage:WaitForChild('Modules')

local Fishing = require(Modules.Fishing)

Fishing module

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

RunService.RenderStepped:Connect(function()
	local NewRay = Ray.new(Character.HumanoidRootPart.CFrame.p, Vector3.new(0, -5, 0))
	local Part = workspace:FindPartOnRayWithIgnoreList(NewRay, {Character})
		
	if Part then
		if Part:IsDescendantOf(FishingSpots) then
			print('Touching part')
		end
	end
end)

The print works when the player first joins, but when you die, it doesn’t print anymore, so I’m guessing it’s using an old character (the dead one) instead of the new one. Makes no sense to me how it could be using the old one as, I set the character at the top of the script so it should only be dealing with the new one

1 Like

The Character variable does not care about the path used to get to the player’s character. Once the variable is set, it will not be set to another object unless you explicitly set it to another object. In your case, it will always refer to the first character it was set to.

Instead of storing the character in a variable, attempt to get the character of the player every render step. If it exists, continue with the ray cast. Otherwise, return from the listener:

local Player = Players.LocalPlayer

RunService.RenderStepped:Connect(function()
	local Character = Player.Character
	if Character == nil then return end
	
	local NewRay = Ray.new(Character.PrimaryPart.Position, Vector3.new(0, -5, 0))
	local Part = workspace:FindPartOnRayWithIgnoreList(NewRay, {Character})
		
	if Part then
		if Part:IsDescendantOf(FishingSpots) then
			print('Touching part')
		end
	end
end)

I also replaced CFrame.p with Position to avoid deprecated members and simplify your code.

1 Like

Worth plugging in that while you can get rid of the Character variable and instead check for the character within the function, you can also handle respawn cases yourself (which is probably what you should be doing). Take for example the PlayerModule that handles the camera and controls. It’s in PlayerScripts meaning the module is non-resetting, but it manually accounts for when it needs to reset in its respective locations.

Example chunk:

https://github.com/CloneTrooper1019/Roblox-Client-Tracker/blob/roblox/scripts/PlayerScripts/StarterPlayerScripts_NewStructure/PlayerModule.module/ControlModule.lua#L363-L387

2 Likes

But if the module is required a second time, doesn’t that mean the new required module should run with the new variables?

1 Like

The module should have the new variables regardless of requiring it a second time.

1 Like

Once a ModuleScript is initially required by a machine, its returned datum is cached. If the same machine requires the same ModuleScript again, the ModuleScript will not be executed again; the call to require just returns the cached result.