Trouble with module script

The script works fine when a character is first added to the game, but the problem occurs when a new character is added.

require(game.ReplicatedStorage.Framework.Client):Inti()

The script above is a script that calls the module loader.

Here is the module loader.

local Client = {}
Client.__index = Client

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local Communication = game.ReplicatedStorage:WaitForChild("Communication")
local Events = Communication:WaitForChild("Events")
local Functions = Communication:WaitForChild("Functions")

function Client:Inti()
	Player.CharacterAdded:Connect(function(Character)
		self:CharacterAdded(Character)
	end)
	delay(0, function()
		local ClientGoreHandler = require(script:WaitForChild("ClientGoreHandler"))
		Events.BloodSpawn.OnClientEvent:Connect(function(position, velocity)
			ClientGoreHandler.CreateBloodDrip(position, velocity)
		end)
	end)
end

function Client:CharacterAdded(Character)
	local self = setmetatable({}, Client)
	self.MeleeClientLoader = require(script:WaitForChild("MeleeClientLoader")):Inti()
	self.DashClientHandler = require(script:WaitForChild("DashClientHandler")):Inti()
	self.SprintClientHandler = require(script:WaitForChild("SprintClientHandler")):Inti()
	self.Character = Character
	self.Humanoid = self.Character:WaitForChild("Humanoid")
	self.PlayerConfigs = self.Character:WaitForChild("PlayerConfigs")

	self.Character.ChildAdded:Connect(function(child)
		if child:IsA("Tool") then
			self.MeleeClientLoader:Equip(child)
		end
	end)
	self.Character.ChildRemoved:Connect(function(child)
		if child:IsA("Tool") then
			self.MeleeClientLoader:Unequip(child)
		end
	end)
	UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
		if gameProcessedEvent then return end
		
		if input.KeyCode == Enum.KeyCode.E then
			self.DashClientHandler:Start(true)
		end
	end)
	
	game:GetService("RunService").Heartbeat:Connect(function()
		if self.PlayerConfigs.PlayerControl.Value == Character.PlayerConfigs.PlayerControl.Value then
			print(self.PlayerConfigs.PlayerControl.Value, Character.PlayerConfigs.PlayerControl.Value)
		else
			print(self.PlayerConfigs.PlayerControl.Value, Character.PlayerConfigs.PlayerControl.Value)
		end
	end)
	return self
end

return Client

It works fine when a character is added to the game for the first time, but when a new character is added, the information of the old character and the new character are displayed together.

3 Likes

Create a variable to store those information and if the new character is added, you can try to remove the old information and replace it with a new one.

2 Likes

example:

local Client = {}
Client.__index = Client

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local MeleeClientLoader = require(script:WaitForChild("MeleeClientLoader"))
local DashClientHandler = require(script:WaitForChild("DashClientHandler"))
local SprintClientHandler = require(script:WaitForChild("SprintClientHandler"))

local Communication = game.ReplicatedStorage:WaitForChild("Communication")
local Events = Communication:WaitForChild("Events")
local Functions = Communication:WaitForChild("Functions")

function Client:Inti()
	Player.CharacterAdded:Connect(function(Character)
		self:CharacterAdded(Character)
	end)
	delay(0, function()
		local ClientGoreHandler = require(script:WaitForChild("ClientGoreHandler"))
		Events.BloodSpawn.OnClientEvent:Connect(function(position, velocity)
			ClientGoreHandler.CreateBloodDrip(position, velocity)
		end)
	end)
end

function Client:CharacterAdded(Character)
	local self = setmetatable({}, Client)

	self.Character = Character
	self.Humanoid = self.Character:WaitForChild("Humanoid")
	self.PlayerConfigs = self.Character:WaitForChild("PlayerConfigs")

	self.MeleeClientLoader = MeleeClientLoader:Inti()
	self.DashClientHandler = DashClientHandler:Inti()
	self.SprintClientHandler = SprintClientHandler:Inti()

	self.Character.ChildAdded:Connect(function(child)
		if child:IsA("Tool") then
			self.MeleeClientLoader:Equip(child)
		end
	end)
	self.Character.ChildRemoved:Connect(function(child)
		if child:IsA("Tool") then
			self.MeleeClientLoader:Unequip(child)
		end
	end)
	UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
		if gameProcessedEvent then return end
		
		if input.KeyCode == Enum.KeyCode.E then
			self.DashClientHandler:Start(true)
		end
	end)
	
	game:GetService("RunService").Heartbeat:Connect(function()
		if self.PlayerConfigs.PlayerControl.Value == Character.PlayerConfigs.PlayerControl.Value then
			print(self.PlayerConfigs.PlayerControl.Value, Character.PlayerConfigs.PlayerControl.Value)
		else
			print(self.PlayerConfigs.PlayerControl.Value, Character.PlayerConfigs.PlayerControl.Value)
		end
	end)
	return self
end

return Client

should i do it like this?

3 Likes

What do those loaders and handlers do?

2 Likes

It’s because this script should be serversided and you should use events to fire events within the serversided script.

1 Like

Ah now I get your problem. I notice that the second player doesn’t have the stamina GUI showing, yet he still can run.

1 Like

yes. We need to solve that problem. :frowning:

1 Like

Weird. I tested it again and both GUI seems to be working.

Oh wait. It is working until I reset one of the characters. Then the GUI don’t seem to work.

Well, it seems to be a problem that is harder to fix than I thought.

I also see your server script having problems changing the character’s stamina after respawn. But I believe it’s due to the client having problems firing the event.

1 Like

I don’t know why. If it has been fixed, please contact me.

You’d have to find a way to get the old object to stop running, because when I respawned the character, the old object is still running. I’m trying to find a way about this situation.

Alternatively, you could update the object to handle the new character.

you are so amazing You found the cause of the problem!

1 Like

However I haven’t fixed it yet so don’t get too excited!

Create a variable called ‘char’ or whatever you want it to call.

Change the callback inside the Player.CharacterAdded event like this:

if not char then -- checks if char doesnt exist
    char = self:CharacterAdded(Character)
else
    -- update all the properties and events
end

Also turn the events you binded into a separate function so you can run it everytime your character gets changed or something.

Just update anything that has something to do with Character

If so, has it been fixed?!!?!!

I fixed the running part but I haven’t fixed other parts yet.