Module scripts work when you join the game, but doesn't work when you respawn back in

Hey.

In studio, when I go to join my game, the module scripts inside of the game work perfectly fine, only if you don’t respawn. When you respawn, everything stops working and I am very confused on why they stop working.

video that shows what happens:
robloxapp-20230116-1028533.wmv (4.8 MB)

Im thinking it might be the script that im loading my modules in? I have no idea. Im still learning lua, so ofcourse i will make little mistakes here and there.

Thank you

Put it inside StarterCharacter instead of StarterPlayer or you will need to run a function on Player.CharacterAdded, this gets fired when the player respawns, because well when the original character dies you should replace it with the new one.

This unfortunately did not work. I even tried editing the function inside of my module script (the function that gets the character) and replaced it with a module.Character = player.Character or player.CharacterAdded:Wait() because it was originally module.Character = player.Character and that still did not work.

Honestly without being able to see the code I cannot do much for you here.

Do you need both of the scripts? The one that loads the module script and the module script its-self?

Depends which one the issue is.

I put the module scripts inside of StarterCharacterScripts and they work perfectly fine now. Even when you respawn they work now. (Solution 1)

local module = {}

local enabled = false

module.Player = game:GetService("Players").LocalPlayer

local function onTouch(object)
	if object:IsA("Part") and object.Name == 'Part' then
		object.BrickColor = BrickColor.Random()
	end
end

local function onCharacterAdded()
   module.Character = module.Player.Character
   module.Humanoid = module.Character:WaitForChild("Humanoid")

	if enabled  then 
		module.Humanoid.Touched:Connect(onTouch)
	end
end

if module.Player.Character then onCharacterAdded(module.Player.Character) end
module.Player.CharacterAdded:Connect(onCharacterAdded)

function module.Touch()
	enabled = true
	if module.Humanoid then 
		module.Humanoid.Touched:Connect(onTouch)
	end
end

return module
1 Like

This works fine too. Thank you! (Solution 2)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.