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

Help with a huge script?

Asked by 9 years ago

I spent a few hours writing the MasterScript for my RPG game I'm working on, but for some reason it doesn't assign all of the values listed in the assignValues function like I tell it to. In the output, it gives me, "Gold is not a valid member of Player" in bold red letters. Here's my code, I'm not sure what's wrong specifically because right now nothing is working (and I know that my Hierarchy is correct): And also, as a note, it is in a LocalScript.

--Player value Variables
Gold = Instance.new("NumberValue")
Gems = Instance.new("NumberValue")
PlayerLevel = Instance.new("NumberValue")
Exp = Instance.new("NumberValue")
HP = Instance.new("NumberValue")
Quest1 = Instance.new("BoolValue")
Quest2 = Instance.new("BoolValue")
Quest3 = Instance.new("BoolValue")
Quest4 = Instance.new("BoolValue")
Quest5 = Instance.new("BoolValue")
Player = game.Players.LocalPlayer
Gold = 10
Gems = 0
PlayerLevel = 1

--Gui frame variables
ScreenGui = game.StarterGui.ScreenGui
ShopBG = ScreenGui.ShopBG
QuestsBG = ScreenGui.QuestsBG
StatsBG = ScreenGui.StatsBG
OptionsBG = ScreenGui.OptionsBG

--Start of functions
wait(3)

function assignValues() --assigns LocalPlayer all necessary values
repeat wait() until Player --waits until Player is loaded before assigning values
    Gold.Parent = Player
    Gold.Name = "Gold"
    Gold.Value = 10
    Gems.Parent = Player
    Gems.Name = "Gems"
    Gems.Value = 0
    PlayerLevel.Parent = Player
    PlayerLevel.Name = "PlayerLevel"
    PlayerLevel.Value = 1
    Exp.Parent = Player
    Exp.Name = "Exp"
    HP.Parent = Player
    HP.Name = "HP"
    Quest1.Parent = Player
    Quest1.Name = "Quest1"
    Quest1.Value = false
    Quest2.Parent = Player
    Quest2.Name = "Quest2"
    Quest2.Value = false
    Quest3.Parent = Player
    Quest3.Name = "Quest3"
    Quest3.Value = false
    Quest4.Parent = Player
    Quest4.Name = "Quest4"
    Quest4.Value = false
    Quest5.Parent = Player
    Quest5.Name = "Quest5"
    Quest5.Value = false
end


while true do
    if Player.Exp.Value >= 100 then
        Player.PlayerLevel.Value = 2
    elseif Player.Exp.Value >= 300 then
        Player.PlayerLeve.Valuel = 3
    elseif Player.Exp.Value >= 600 then
        Player.PlayerLeve.Valuel = 4
    elseif Player.Exp.Value >= 1000 then
        Player.PlayerLevel.Value = 5
    elseif Player.Exp.Value >= 1500 then
        Player.PlayerLevel.Value = 6
    elseif Player.Exp.Value >= 2000 then
        Player.PlayerLevel.Value = 7
    elseif Player.Exp.Value >= 2500 then
        Player.PlayerLevel.Value = 8
    elseif Player.Exp.Value >= 3000 then
        Player.PlayerLevel.Value = 9
    elseif Player.Exp.Value >= 4000 then
        Player.PlayerLevel.Value = 10
    elseif Player.Exp.Value >= 5000 then
        Player.PlayerLevel.Value = 11
    elseif Player.Exp.Value >= 6500 then
        Player.PlayerLevel.Value = 12
    elseif Player.Exp.Value >= 8000 then
        Player.PlayerLevel.Value = 13
    elseif Player.Exp.Value >= 9500 then
        Player.PlayerLevel.Value = 14
    elseif Player.Exp.Value >= 11000 then
        Player.PlayerLevel.Value = 15
    elseif Player.Exp.Value >= 12500 then
        Player.PlayerLevel.Value = 16
    elseif Player.Exp.Value >= 14000 then
        Player.PlayerLevel.Value = 17
    elseif Player.Exp.Value >= 15500 then
        Player.PlayerLevel.Value = 18
    elseif Player.Exp.Value >= 18000 then
        Player.PlayerLevel.Value = 19
    elseif Player.Exp.Value >= 21000 then
        Player.PlayerLevel.Value = 20
    elseif Player.Exp.Value >= 25000 then
        Player.PlayerLevel.Value = 21
    elseif Player.Exp.Value >= 30000 then
        Player.PlayerLevel.Value = 22
    elseif Player.Exp.Value >= 35000 then
        Player.PlayerLevel.Value = 23
    elseif Player.Exp.Value >= 40000 then
        Player.PlayerLevel.Value = 24
    elseif Player.Exp.Value >= 45000 then
        Player.PlayerLevel.Value = 25
    elseif Player.Exp.Value >= 50000 then
        Player.PlayerLevel.Value = 26
    elseif Player.Exp.Value >= 57500 then
        Player.PlayerLevel.Value = 27
    elseif Player.Exp.Value >= 65000 then
        Player.PlayerLevel.Value = 28
    elseif Player.Exp.Value >= 72500 then
        Player.PlayerLevel.Value = 29
    elseif Player.Exp.Value >= 82500 then
        Player.PlayerLevel.Value = 30
    elseif Player.Exp.Value >= 100000 then
        Player.PlayerLevel.Value = 31
    else
    print("An error has occured with the LevelUp Function.")
    end
end

while true do
    if Player.PlayerLevel.Value == 1 then
        Player.HP.Value = 300
    elseif Player.PlayerLevel.Value == 2 then
        Player.HP.Value = 400
    elseif Player.PlayerLevel.Value == 3 then
        Player.HP.Value = 500
    elseif Player.PlayerLevel.Value == 4 then
        Player.HP.Value = 600
    elseif Player.PlayerLevel.Value == 5 then
        Player.HP.Value = 700
    elseif Player.PlayerLevel.Value == 6 then
        Player.HP.Value = 800
    elseif Player.PlayerLevel.Value == 7 then
        Player.HP.Value = 900
    elseif Player.PlayerLevel.Value == 8 then
        Player.HP.Value = 1000
    elseif Player.PlayerLevel.Value == 9 then
        Player.HP.Value = 1100
    elseif Player.PlayerLevel.Value == 10 then
        Player.HP.Value = 1200
    elseif Player.PlayerLevel.Value == 11 then
        Player.HP.Value = 1300
    elseif Player.PlayerLevel.Value == 12 then
        Player.HP.Value = 1400
    elseif Player.PlayerLevel.Value == 13 then
        Player.HP.Value = 1500
    elseif Player.PlayerLevel.Value == 14 then
        Player.HP.Value = 1600
    elseif Player.PlayerLevel.Value == 15 then
        Player.HP.Value = 1700
    elseif Player.PlayerLevel.Value == 16 then
        Player.HP.Value = 1800
    elseif Player.PlayerLevel.Value == 17 then
        Player.HP.Value = 1900
    elseif Player.PlayerLevel.Value == 18 then
        Player.HP.Value = 2000
    elseif Player.PlayerLevel.Value == 19 then
        Player.HP.Value = 2100
    elseif Player.PlayerLevel.Value == 20 then
        Player.HP.Value = 2200
    elseif Player.PlayerLevel.Value == 21 then
        Player.HP.Value = 2300
    elseif Player.PlayerLevel.Value == 22 then
        Player.HP.Value = 2400
    elseif Player.PlayerLevel.Value == 23 then
        Player.HP.Value = 2500
    elseif Player.PlayerLevel.Value == 24 then
        Player.HP.Value = 2600
    elseif Player.PlayerLevel.Value == 25 then
        Player.HP.Value = 2700
    elseif Player.PlayerLevel.Value == 26 then
        Player.HP.Value = 2800
    elseif Player.PlayerLevel.Value == 27 then
        Player.HP.Value = 2900
    elseif Player.PlayerLevel.Value == 28 then
        Player.HP.Value = 3000
    elseif Player.PlayerLevel.Value == 29 then
        Player.HP.Value = 3100
    elseif Player.PlayerLevel.Value == 30 then
        Player.HP.Value = 3200
    elseif Player.PlayerLevel.Value == 31 then
        Player.HP.Value = 3300
    else
    print("An error has occured with the byLevelHP Function.")
    end
end

function assignHP()
    Player.MaxHealth = Player.HP.Value
    Player.Health = Player.HP.Value
end


game.Players.PlayerAdded:connect(assignValues)
game.Players.PlayerAdded:connect(assignHP)


2 answers

Log in to vote
0
Answered by
RedCombee 585 Moderation Voter
9 years ago

This wouldn't even run properly. You can't have two while loops running in the same function.

Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

There are many things wrong with this piece of code.

One) That is a horrible way to code this.

USE A TABLE. THAT IS WHY WE HAVE LISTS. GOODNESS.

The HP doesn't even need a table!

Lines 191 to 127 can be replaced with:

Player.HP.Value = 200 + 100 * Player.PlayerLevel.Value;

64 fewer lines!!!!


Similarly...

local xps = {0, 100, 300, 600, 1000, 1500, 2000, 2500, 3000, 4000, 5000, 6500, 8000, 9500, 11000, 12500, 14000, 15500, 18000, 21000, 25000, 30000, 35000, 40000, 45000, 50000, 57500, 65000, 72500, 82500, 100000};
local xp = Player.Exp.Value;
local level = 1;
while xps[level] and xps[level] <= xp do
    level = level + 1;
end

Replaces lines 123 to 61. 56 few lines!!!


There were several typos in all of the junk you had before. That's why you don't want to do that.

Debug this much simpler thing first.


Two) You cannot concurrently run two while loops.

Execution happens in succession; the first loop will continue and never stop.

Interleave the contents into a single while loop.

Similarly, the connection has to be executed before the loop, as the loop will not end.


Three) You are using both LocalPlayer and PlayerAdded.

You are reassigning the local player's leaderstats any time anyone else joins. That is definitely wrong. Generating leaderestats is not something a LocalScript has business doing. Do that with a separate server Script.

Four) Nothing in this script can trigger the error you posted;

something else is erroring (probably because this script is wrong in the above stated ways; the error is caused by reason Two + Three probably). Please actually try to debug wholeheartedly to help us: give the error but also understand it, e.g., it isn't from this script!

0
So I should write a separate assignValues script in the Workspace rather than including it in the MasterScript? SlickPwner 534 — 9y
0
Also, wouldn't the HP script you wrote multiply 300 by the PlayerLevel each time? I want it to increase by 100 each time the player Levels up. SlickPwner 534 — 9y

Answer this question