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

How to change MaxHealth?

Asked by
KenUSM 53
4 years ago
Edited 4 years ago

I keep getting this error

Workspace.ServerScript:98: attempt to index nil with 'MaxHealth'

the error is coming from this function

function ImplimentData(Player, Humanoid)
    for RankName, RankTable in pairs(DataModule.Rank) do 
        if Update == false then
            if RankTable.Name == Player.Data.Rank.Value then 
                Humanoid.MaxHealth = RankTable.MaxHealth -- this is line 98
            end
        else
            if RankTable.Name == Player.Data.Rank.Value then 
                if Player.Data.Exp.Value >= RankTable.ExpLimit then 
                    local Difference = Player.Data.Exp.Value - RankTable.ExpLimit
                    UpdateRankData(Player, RankTable.NextRank)
                    Player.Data.Exp.Value = Difference
                    UpdateExpData(Player, Player.Data.Exp.Value)
                end
            end
        end
        Update = false
    end
end

I identified Humanoid in lines 201 - 209

Players.PlayerAdded:Connect(function(Player) -- lines 201
    DataCreate(Player)
    GetData(Player)
    Player.CharacterAdded:Connect(function(Character)
        repeat wait() until Character:FindFirstChild("Humanoid")
        local Humanoid = Character.Humanoid
        ImplimentData(Player, Humanoid)
    end)
end)  -- lines 209

DataModule is a ScriptModule in ServerStorage I named it DataModule in ServerStorage but the table name is DataProperties. This is the code

local DataProperties = {
    Rank = {
        F = {
            Name = "F",
            MaxHealth = 150,
            ExpLimit = 1000,
            NextRank = "E"
        };
        E = {
            Name = "E",
            MaxHealth = 200,
            ExpLimit = 2000,
            NextRank = "D"
        };
        D = {
            Name = "D",
            MaxHealth = 250,
            ExpLimit = 3000,
            NextRank = "C"
        };
        C = {
            Name = "C",
            MaxHealth = 300,
            ExpLimit = 4000,
            NextRank = "B"
        };
        B = {
            Name = "B",
            MaxHealth = 400,
            ExpLimit = 5000,
            NextRank = "A"
        };
        A = {
            Name = "A",
            MaxHealth = 500,
            ExpLimit = 100000000000,
        };

    };

}

return DataProperties;

Can anyone help me with the error is it because of placement, because i already tried changing the position of the function in the script i still retrieved the same error. Thanks for review my script, your help is appreciated.

0
Where does DataModule.Rank get set? SteamG00B 1633 — 4y
0
It is set in a ScriptModule in Server Storage, the code for it is local DataProperties = { Rank = { F = { Name = "F", MaxHealth = 150, ExpLimit = 1000, NextRank = "E" }; return DataProperties; I named the script DataModule but the table DataProperties KenUSM 53 — 4y
0
DataProperties = { Rank = { F = { Name = "F", MaxHealth = 150, ExpLimit = 1000, NextRank = "E" }; }; return DataProperties;* KenUSM 53 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

I was going to talk to you in community chat in this, but I decided I'd make a post because I've gotta go to bed soon. There's a lot going on here.

When did you call upon the function? you used humanoid as a variable before it was defined. Then you define it WAY later in the script. But you defined it locally. That's my issue.

The error is coming up saying that you are associating MaxHealth with nil, and nil just doesn't exist.

Without the whole script there's, not a lot I see myself doing. I did some testings your humanoid is correctly defined, but based on logic, since it was defined on line 206 and not 98 or before 98 that might be the problem.

Try defining humanoid later before line 98.

1
I tried but didn't work I changed majority of the code and moved mostly the essential parts into a modulescript. I ran into another problem. once i fix that ill post my new script KenUSM 53 — 4y
1
I fixed it I just made sure that humanoid valid in all instances where the function is called KenUSM 53 — 4y
0
glad you got it. great job. Sorry I wasn't much use. But I can recommend you have a repeat until loop. While that is nice, why not use :WaitForChild("Humanoid") on line 205? legoguy939 418 — 4y
Ad

Answer this question