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

Help with ModuleScript Error?

Asked by 8 years ago

Hello, below is a module script I am using to calculate a vehicles "Health." I am receiving an error that is also shown below. I've attempted to resolve it but to no avail so any help you guys could provide is appreciated, Thanks

wait(.1)
local HealthHandler = {}
local Player = game.Players.LocalPlayer

function HealthHandler.Car(Health,MaxHealth)
    print(Player.PlayerGui.HealthDisplay.Name)
    local GUI = Player.PlayerGui.HealthDisplay

    GUI.Backround.HealthFrame.Health.Size = UDim2.new(Health/MaxHealth, 0, 1, 0)
end


return HealthHandler

Here's the error: 22:26:23.949 - ServerScriptService.HealthHandler:6: attempt to index upvalue 'Player' (a nil value) 22:26:23.953 - Stack Begin 22:26:23.954 - Script 'ServerScriptService.HealthHandler', Line 6 - field Car

Note: the script works fine in studio.

1
Are you require()ing this from a LocalScript or a Script? adark 5487 — 8y
0
Just a regular script lordrex12345 25 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

Heyo,

Ok, so before we do something,

Why do we use ModuleScripts? Module scripts are meant to store certain values, functions and methods which could be used globally In-Game. In the script that you have given, You are trying to access the client via a Module script, which is bad way of using them.

Module scripts are just there to make your life easy so, Here's how one could use them for...

Locally (Module scripts stored in ReplicatedStorage, so they can be accessed locally) - A UI manipulator which will fasten stuff like, GUI fading animations or Tween effects etc. - For Camera Manipulation, Basically you make a library with lots of methods that apply to the Camera object, inturn you can use this kind of module to work on Camera Manipulation

Server (They can be stored in ServerStorage or in your ROBLOX inventory and called via require() function) - The game script, Controls the Map loading, Weapons etc - For making scripts for Vehicles, or for Leaderboards

There are alot of applications of them, but you need to remember how you use them For Example

Here's a script which will capture flag when a player touches it and then updates the GUI on his screen(Not tested in studio)

We first place a Remote event in ReplicatedStorage...

ModuleScript -- Plced inside the ReplicatedStorage so both (Client and Server) have access

local Event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
local module = {}


    module.AlertPlayer(gui, Player) -- This will be run on a local script since we can only manipulate guis via a Local script.
        gui.Visible = true -- Set the gui visible 
    end


    module.CaptureFlag(Flag_Trigger, Player) -- Define our function, In this, We try to get the flag that player touched, and which player has touched the flag as arguements
        Flag_Trigger.Captured = true
        event:FireClient(Player) -- We send the client our gratitudes xD 
    end    
return module

SeverScript

local flag = game.Workspace.Part -- Imagine this part to be our flag :O
local module = game:GetService("ReplicatedStorage"):FindFirstChild("ModuleScript")
local event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") -- We can access it because it's replicated on both the client and the server

flag.Touched:connect(function(hit)
    if hit and hit:FindFirstChild("Humanoid") and hit:FindFirstChild("Humanoid").Health > 0 then
        local Character = hit.Parent
        local Player = game:GetService("Players"):GetPlayerFromCharacter(Character)

       local module.CaptureFlag(flag, Player) -- We ru nthe function defined in the module script.
    end
end)

LocalScript -- Basically in the Player's GUI or in Player Object =3

local module = game:GetService("ReplicatedStorage"):FindFirstChild("ModuleScript")
local event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
local GUI = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui"):FindFirstChild("AlertGui")


 module.AlertPlayer(GUI, game:GetService("Players").LocalPlayer)


Well, I know this doesn't fix your problem, but what it does is gives you an Idea on how to use the Module scripts along with Local and Serverscripts when FE is enabled, I may have rushed it a bit, or I could have solved that though Im too lazy ;-; (Lazy fox hip dance)

Anyways, Hope that helps you out in one way or the other ;) Cheers!

Ad

Answer this question