#module scripts don't reload if player dies?

1 messages · Page 1 of 1 (latest)

idle linden
#
--("client") localscript in starterplayerscripts:
local Players = game:GetService("Players")
local player = Players.LocalPlayer

player.CharacterAdded:Connect(function()
  print("respawn test")
  local respawntest = require(player.PlayerScripts:WaitForChild("ModuleScript")
end)
---------------- end script


--("ModuleScript") modulescript in starterplayerscripts:
local respawntest
print("respawn test module")
return respawntest
---------------- end script

Okay
i start game and my player spawns.
output:

respawn test
respawn test module

Then
i reset my character or die and respawn.
output:

respawn test

Why cant i reload module scripts??

rocky gyro
#

it doesnt get required again

#

Its just same copy that already ran

#

if i understood you correctly

idle linden
#

how do i fix this for respawning characters

#

i just cleaned up the monolith of a script that the client script was into modules

rocky gyro
#

what exacly you need

idle linden
#

okay so i need all the scripts inside the modules to reset on respawn

idle linden
#

basically i want

respawn test module

to print when i respawn

rocky gyro
#

no

#

Terribleeeee

#

Make the whole module into single function

#

and just call that

#

when you want to reload

#

¯_(ツ)_/¯

idle linden
#

im sorry im dumb please give me an example

rocky gyro
idle linden
#

yes

rocky gyro
#

and you want it to reset on respawn

#

so u just do

#

a function lets say
module.respawn()
print("hello my friend")
end

#

if you want it to restart completly you prob need to clean up variables

#

or disconnect stuff

#

if you connect

idle linden
#

its already very very messy because i only just started using modules and i basically used to have one monolith client script with over 100 functions doing random stuff and now it looks like this

player.CharacterAdded:Connect(function()
    local Character = player.Character

    local FurnaceClient = require(player.PlayerScripts:WaitForChild("FurnaceClient"))
    local PlacementClient = require(player.PlayerScripts:WaitForChild("PlacementClient"))
    local TableClient = require(player.PlayerScripts:WaitForChild("TableClient"))
    local LeaderstatsClient = require(player.PlayerScripts:WaitForChild("LeaderstatsClient"))
    local MessageClient = require(player.PlayerScripts:WaitForChild("MessageClient"))
    local TutorialClient = require(player.PlayerScripts:WaitForChild("TutorialClient"))
    local CrateClient = require(player.PlayerScripts:WaitForChild("CrateClient"))
    local InventoryClient = require(player.PlayerScripts:WaitForChild("InventoryClient"))
    local PianoClient = require(player.PlayerScripts:WaitForChild("PianoClient"))
    local RemoveClient = require(player.PlayerScripts:WaitForChild("RemoveClient"))
end)
#

not exactly some connections are in the client directly

rocky gyro
#

bro

#

wtf are you doing

#

this is sucha mess

#

recode all so you dont need to re require all

#

make functions in each that you fire after respawn

idle linden
#

ive been coding for 6 months dude i understand this is a mess

rocky gyro
#

but dont constantly require

idle linden
#

i just put all the relevent functions into module scripts.... so they were easier to follow

#

alot of the functions wont just run by themselves

#

thats why i just 'required' the whole module

#

so it wasnt a 800 line client script

#

and parts were also in playerGUI aswell

#

i wanted to clean it up

#

@rocky gyro So basically do you want me to go through everything and look for every specific function i need to run on the client on respawn?

#

and just call those functions directly into the client script?

rocky gyro
#

ig

idle linden
#

lol fuck

#

btw ive been working on this for months im not going to just 'recode all' because i dont have time.. is there an 'easy fix' that i could use instead

#

because at this point im about to turn the client script back into a monolith

rocky gyro
#

Hold up

#

let me try smth

idle linden
#

i could turn every module into a giant function and call the whole thing

#

LOL

#

im sorry thankyou btw

rocky gyro
#

That might work depending on your code

#

is simply putting whole module into a function

#

so the whole ass module is one giant function

idle linden
#

HAHAHHAA

rocky gyro
#

which you can call over and over to rerun everything

idle linden
#

yeah ill give it a go

rocky gyro
#

This can not work if youre using events tho

#

cuz it will not disconnects lets say .touched event

#

you would have to manually do that

idle linden
#

i know that nesting events suck because they run multiple times if you call them

#

im still learning that i have to use connection = something.Connect(function.. etc

#

and then connection:Disconnect()

#

it seemed to work before but i could of ruined everything in the process of turning them into modules in the first place cuz some things were in starterGUI and other parts were in the monolith starterplayer script

idle linden
#

i might be a while cheers for the help btw @rocky gyro

rocky gyro
#

Goodluck bro

vagrant basin
rocky gyro
#

its cached

#

already

#

so wont run 2nd time

vagrant basin
#

also why not just put your script in startercharacterscripts

vagrant basin
rocky gyro
#

no like

#

his whole reason is

vagrant basin
#
player.CharacterAdded:Connect(function(char)
    for _,v in char:GetChildren() do
        if v:IsA("ModuleScript") and string.find(v.Name, "ModuleScript") then
            require(v).Test()
        end
    end
end)
rocky gyro
#

to re require

#

modules

vagrant basin
#

oh

rocky gyro
#

aka reload them

#

which doesnt work

#

as its cached

vagrant basin
#

maybe i don't understand wym by reload

#

maybe handle CharacterAdded inside of a module?

idle linden
#

could i do that? handle character added inside the module? it would mean i would have like 10 characteradded:Connect functions running all at once

idle linden
#

For anyone who cares i made an update

i found that what i had to do is first move ALL standalone ":Connect" functions directly into the localscript and pass the character from the "CharacterAdded" event into the :Connect function. As before i would reference the Character inside the modulescript and it would not recognize it after resetting or death.

for example

local ReplicatedStorage = game:GetService("ReplicatedStorage")

player.CharacterAdded:Connect(function()
  local Character = player.Character      -- The character will refresh every time the player resets
  local someModule = require(player.PlayerScripts:WaitForCHild("ModuleClient")

  local Event = ReplicatedStorage.Events.fireEvent

  Event:OnClientEvent:Connect(function()
    someModule:InitiateThingy(Character)  -- Pass the Character in so the modulescript doesnt try to 
                                          -- reference the 'old' character if the player dies or resets.
  end)
end)
#

yes im doing it the slow way i have tried a few 'quick fixes' but none worked and was looking stupid as fuck lol.

idle linden
#

soo... i have been working on this all day and i broke something.... it keeps double firing even though there are no nested :Connect functions... i have no idea why or how to fix it and when i respawn or reset character it still double fires and gives me errors even though it shows up anyway. for example - an error i'd get is "guiframe is not valid member of screengui"... but then it will show the gui frame anyway like its double firing and the second time is just erroring or something this is getting difficult to understand whats going on at this point.