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

Getting Local Player with Module Script?

Asked by 4 years ago

Hi Everyone!

I've been recently working on my game and was adding somethings to my main game script, when all of a sudden a module script doesn't get the player.

Here's the main game script

Main = require(game.Workspace.ModuleScripts.Main)
Shop = require(game.Workspace.ModuleScripts.Shop)
local status = game.ReplicatedStorage:WaitForChild("Status")
local intermissionTime = 7
local roundTime = 120
local players = game:GetService("Players"):GetPlayers()
local SwordAccess = game:GetService("ReplicatedStorage").Swords
local Number = intermissionTime
local minplayersReq = 1
local playersinround = players
local SwordChosen = nil

--||Swords||--
local sword = SwordAccess.BasicSword
local speedySword = nil -- dont mind this nil, i just havent found a sword yet
local epicSword = nil -- dont mind this nil, i just havent found a sword yet
local AzureSword = SwordAccess.AzureSword
local OverseerSword = SwordAccess.OverseerSword

--||Which Sword "if" statement||--
if Shop.BSequipped == true then
    SwordChosen = "sword"
elseif Shop.SSequipped == true then
    SwordChosen = "speedysword"
elseif Shop.ESequipped == true then
    SwordChosen = "epicsword"
elseif Shop.ASequipped == true then
    SwordChosen = "azuresword"
elseif Shop.OSequipped == true then
    SwordChosen = "overseersword"
end

while true do
    for i = 1, intermissionTime, 1 do
        status.Value = "INTERMISSION (" .. Number .. ")"
        wait(1)
        Number = Number - 1
        if Number == 5 then
            players = game:GetService("Players"):GetPlayers()
            playersinround = players
            Main.CharacterList = playersinround
        end
    end

    if #playersinround >= minplayersReq then
        Number = roundTime
        Main.ToBattleMap()

        players = game:GetService("Players"):GetPlayers()
        for i = 1, #players, 1 do
            player = players[i]
            if player:FindFirstChild("Sword") == nil then
                local swordval = Instance.new("StringValue")
                swordval.Name = "Sword"
                swordval.Parent = player
                swordval.Value = SwordChosen
            end

            local backpack = player:WaitForChild("Backpack")
            if backpack then
                local swordClone = nil
                if player.Sword.Value == "sword" then
                    swordClone = sword:Clone()
                elseif player.Sword.Value == "speedysword" then
                    swordClone = speedySword:Clone()
                elseif player.Sword.Value == "epicsword" then
                    swordClone = epicSword:Clone()
                elseif player.Sword.Value == "azuresword" then
                    swordClone = AzureSword:Clone()
                elseif player.Sword.Value == "overseersword" then
                    swordClone = OverseerSword:Clone()
                end
                swordClone.Parent = backpack
            end
        end

        for i = 1, roundTime, 1 do
            status.Value = "ROUND IN PROGRESS (" .. Number .. ")"
            wait(1)
            Number = Number - 1
        end
        Main.ToLobbyMap()

        players = game:GetService("Players"):GetPlayers()
        for i = 1, #players, 1 do
            local player = players[i]
            local swordClone = player.Backpack:FindFirstChild("BasicSword")
            if swordClone ~= nil then
                local DeleteWeapons = player.Backpack:GetChildren()
                for i = 1, #DeleteWeapons, 1 do
                    weapon = DeleteWeapons[i]
                    weapon:Destroy()
                end
            elseif swordClone == nil then
                for _,v in pairs(player.Character:GetChildren()) do
                    if v:IsA("Tool") then
                        v:Destroy()
                    end
                end
            end
        end

    else
        status.Value = "COULD NOT START GAME, TOO LITTLE PLAYERS"
        wait(5)
    end
    Number = intermissionTime
end

And here's my "Shop" Module Script

local Shop = {}

local SwordReference = game.ReplicatedStorage.ShopItems.Swords
local SpdBuffReference = game.ReplicatedStorage.ShopItems.SpeedBuffs

local players = game:GetService("Players")
local player = players.LocalPlayer

local Stuff = player.PlayerGui:WaitForChild("Bank").Background.ShopBackground.ItemsBackground
local shop = player.PlayerGui:WaitForChild("Bank")

--||Swords||--

Shop.BSequipped = SwordReference.BasicSword.Equipped.Value

Shop.SSequipped = SwordReference.SpeedySword.Equipped.Value
Shop.SSbought = SwordReference.SpeedySword.Bought.Value
Shop.SScost = SwordReference.SpeedySword.Cost.Value

Shop.ESequipped = SwordReference.EpicSword.Equipped.Value
Shop.ESbought = SwordReference.EpicSword.Bought.Value
Shop.EScost = SwordReference.EpicSword.Cost.Value

Shop.ASequipped = SwordReference.AzureSword.Equipped.Value
Shop.ASbought = SwordReference.AzureSword.Bought.Value
Shop.AScost = SwordReference.AzureSword.Cost.Value

Shop.OSequipped = SwordReference.OverseerSword.Equipped.Value
Shop.OSbought = SwordReference.OverseerSword.Bought.Value
Shop.OScost = SwordReference.OverseerSword.Cost.Value

--||Speed Buffs||--

Shop.RSpdequipped = SpdBuffReference.RegSpeed.Equipped.Value

Shop.Spd18equipped = SpdBuffReference.Spd18.Equipped.Value
Shop.Spd18bought = SpdBuffReference.Spd18.Bought.Value
Shop.Spd18cost = SpdBuffReference.Spd18.Cost.Value

Shop.Spd20equipped = SpdBuffReference.Spd20.Equipped.Value
Shop.Spd20bought = SpdBuffReference.Spd20.Bought.Value
Shop.Spd20cost = SpdBuffReference.Spd20.Bought.Value

Shop.Spd23equipped = SpdBuffReference.Spd23.Equipped.Value
Shop.Spd23bought = SpdBuffReference.Spd23.Bought.Value
Shop.Spd23cost = SpdBuffReference.Spd23.Cost.Value

Shop.Spd26equipped = SpdBuffReference.Spd26.Equipped.Value
Shop.Spd26bought = SpdBuffReference.Spd26.Bought.Value
Shop.Spd26cost = SpdBuffReference.Spd26.Cost.Value

--||Show Sections||--
function Shop.ShowWeapons()
    if shop.Enabled == false then
        shop.Enabled = true
    end
    Stuff.Weapons.Visible = true
    Stuff.SpeedBuffs.Visible = false
    Stuff.RSItems.Visible = false
end

function Shop.ShowSpeedBuffs()
    if shop.Enabled == false then
        shop.Enabled = true
    end
    Stuff.Weapons.Visible = false
    Stuff.SpeedBuffs.Visible = true
    Stuff.RSItems.Visible = false
end

function Shop.ShowRSItems()
    if shop.Enabled == false then
        shop.Enabled = true
    end
    Stuff.Weapons.Visible = false
    Stuff.SpeedBuffs.Visible = false
    Stuff.RSItems.Visible = true
end

function Shop.HideShop()
    shop.Enabled = false
end

return Shop

In the end, the module script gives me this error: Workspace.ModuleScripts.Shop:9: attempt to index local 'player' (a nil value) And therefore my main game script experiences an error: Requested module experienced an error while loading

I assume it's because the module script cant access the local player, but it was working fine before, and i don't know another way to reference local player through a module script.

Any help is appreciated :)

0
You can't call LocalPlayer from a Module Script. killerbrenden 1537 — 4y
0
You can do, game.Players.PlayerAdded:Connect(function(player), there's more than just that, but yeah. killerbrenden 1537 — 4y
0
I'm not sure if you can do that in a modulescript since it think it needs another script to execute whatever is in the module 123nabilben123 499 — 4y
0
You can ^^ royaltoe 5144 — 4y
0
Well, we learn new things everyday! killerbrenden 1537 — 4y

1 answer

Log in to vote
1
Answered by
Fifkee 2017 Community Moderator Moderation Voter
4 years ago

ModuleScripts adopt the script type of the script requiring it. If you require a ModuleScript with a localscript, it's type will be a localscript. If you require a ModuleScript with a server script, it's type will be a server script. Meaning, yes, you can call game.Players.LocalPlayer from a modulescript. I even do it several times myself, actually. About 64 times over. Be sure that you're requiring a ModuleScript from a LocalScript, and not a ServerScript.

Cheers.

0
i am using both a server script and a local script with this module. What do i do then? JustAnotherGaming 48 — 4y
0
Use two different modulescripts or check if LocalPlayer is a valid path to differentiate elevations. Fifkee 2017 — 4y
Ad

Answer this question