Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Module not requiring, help?

Asked by 3 years ago
Edited 3 years ago

idk why but the playeradded event don t fire, its located in ServerScriptService and its a server script. i tried different times on reworking the function but it still do nt fire in both game and studio.

Edit: i tested alot of things and found out that the code stop when i require the upgrade manager so now im looking to fix this

-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local Players = game:GetService("Players")

-- Modules
local ModuleScripts = ReplicatedStorage.Modules
local GameSettings = require(ModuleScripts.GameSettings)
local PlayerManagement = ServerScriptService.PlayerManagement
local PlayerManagementModules = PlayerManagement.ModuleScripts
local Data = ServerScriptService.Data
local DataStore = require(Data.Modules.DataStore)
local UpgradeManager = require(Data.Modules.UpgradeManager)

-- Local Functions
local function setupPlayerStats(player)
    local character = player.Character or player.CharacterAdded:wait()
    local humanoid = character:WaitForChild("Humanoid") 

    local LeaderStats = Instance.new("Folder",player)
    LeaderStats.Name = "leaderstats"

    local OtherStats = Instance.new("Folder",player)
    OtherStats.Name = "OtherStats"

    local Money = Instance.new("IntValue",LeaderStats)
    Money.Name = "Money"

    local Reborn = Instance.new("IntValue",LeaderStats)
    Reborn.Name = "Reborn"

    local Cropfarmed = Instance.new("IntValue",LeaderStats)
    Cropfarmed.Name = "Crop Farmed"

    local Vip = Instance.new("BoolValue",OtherStats)
    Vip.Name = "Vip"

    local Donator = Instance.new("BoolValue",OtherStats)
    Donator.Name = "Donator"

    local Tutorial = Instance.new("BoolValue",OtherStats)
    Tutorial.Name = "Tutorial"

    local Pets = Instance.new("Folder",OtherStats)
    Pets.Name = "Pets"

    local Wheat = Instance.new("IntValue",OtherStats)
    Wheat.Name = "Wheat"

    local Carrots = Instance.new("IntValue",OtherStats)
    Carrots.Name = "Carrots"

    local Potatoes = Instance.new("IntValue",OtherStats)
    Potatoes.Name = "Potatoes"

    local Multyplier = Instance.new("IntValue",OtherStats)
    Multyplier.Name = "Multyplier"

    print("work")

    UpgradeManager:Update(player)
end

Players.PlayerAdded:Connect(setupPlayerStats)
0
check if it errors VerdommeMan 1479 — 3y
0
No errors related to this script ,the only errors i get are the one that do nt find the objects that this script make Berta012oof 0 — 3y
0
Posted a answer yHasteeD 1819 — 3y
0
Accept yHasteeD answer if it answered your question. Do it by pressing the accept button below his answer. Zeuxulaz 148 — 3y
0
read comments of the answer Berta012oof 0 — 3y

1 answer

Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
3 years ago
Edited 3 years ago

The script are yielding and the event connect after the player joins, then the event will not get fired, to fix that you need to do a generic for loop within all players that joined before the event fires, here is a example:

wait(2)

local Players = game:GetService('Players')

local function onPlayerAdded(player)
    print(player.Name, 'Joined!')
end

Players.PlayerAdded:Connect(onPlayerAdded)
for _, player in pairs(Players:GetPlayers()) do
    print(player.Name, 'is already in game, calling onPlayerAdded!')
    coroutine.wrap(onPlayerAdded)(player) -- start a new thread and call the function!
end

this will solve the problem

Fixed Code:

-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local Players = game:GetService("Players")

-- Modules
local ModuleScripts = ReplicatedStorage.Modules
local GameSettings = require(ModuleScripts.GameSettings)
local PlayerManagement = ServerScriptService.PlayerManagement
local PlayerManagementModules = PlayerManagement.ModuleScripts
local Data = ServerScriptService.Data
local DataStore = require(Data.Modules.DataStore)
local UpgradeManager = require(Data.Modules.UpgradeManager)

-- Local Functions
local function setupPlayerStats(player)
    local character = player.Character or player.CharacterAdded:wait()
    local humanoid = character:WaitForChild("Humanoid") 

    local LeaderStats = Instance.new("Folder",player)
    LeaderStats.Name = "leaderstats"

    local OtherStats = Instance.new("Folder",player)
    OtherStats.Name = "OtherStats"

    local Money = Instance.new("IntValue",LeaderStats)
    Money.Name = "Money"

    local Reborn = Instance.new("IntValue",LeaderStats)
    Reborn.Name = "Reborn"

    local Cropfarmed = Instance.new("IntValue",LeaderStats)
    Cropfarmed.Name = "Crop Farmed"

    local Vip = Instance.new("BoolValue",OtherStats)
    Vip.Name = "Vip"

    local Donator = Instance.new("BoolValue",OtherStats)
    Donator.Name = "Donator"

    local Tutorial = Instance.new("BoolValue",OtherStats)
    Tutorial.Name = "Tutorial"

    local Pets = Instance.new("Folder",OtherStats)
    Pets.Name = "Pets"

    local Wheat = Instance.new("IntValue",OtherStats)
    Wheat.Name = "Wheat"

    local Carrots = Instance.new("IntValue",OtherStats)
    Carrots.Name = "Carrots"

    local Potatoes = Instance.new("IntValue",OtherStats)
    Potatoes.Name = "Potatoes"

    local Multyplier = Instance.new("IntValue",OtherStats)
    Multyplier.Name = "Multyplier"

    print("work")

    UpgradeManager:Update(player)
end

Players.PlayerAdded:Connect(setupPlayerStats)
for _, player in pairs(Players:GetPlayers()) do
    coroutine.wrap(setupPlayerStats)(player)
end

Hope it helped! :)

0
Thanks so much Berta012oof 0 — 3y
0
Okay tested it but no changes it still not fire / the objects such as leaderstats are not there Berta012oof 0 — 3y
0
so i played a bit with your code and figured out this, your example not fire when in a folder in serverscriptservice, i tried to use the fixed code you made and still do nt work so i tried to test use some wait around but still not working , i have no clue Berta012oof 0 — 3y
0
Maybe any of your modules start a infinite loop, starting the infinite loop will not run the main code yHasteeD 1819 — 3y
Ad

Answer this question