Tutorial - Starting an RPG - Classes + Implementing Statistics


  • To begin, we need a plan. I'm going to make my RPG with 3 classes and then just create some pseudo code to get an idea of the stats of the classes:

    Mage - 85 Health, 10 Defense, Weapons: Fire Spell, Freeze Spell, 17 Speed
    Warrior - 100 Health, 7 Defense, Weapons: Sword, Shield, 15 Speed
    Dwarf - 92 Health, 5 Defense, Weapons: Hammer, Axe, 20 Speed

    Now, I would convert this into a dictionary so that it can be used by lua:

    classes = {
    	["Mage"] = {
    		["Name"] = "Mage";
    		["Health"] = 85;
    		["Defense"] = 10;
    		["Weapons"] = {"Fire Spell", "Freeze Spell"};
    		["Speed"] = 17;
    	};
    	["Warrior"] = {
    		["Name"] = "Warrior";
    		["Health"] = 100;
    		["Defense"] = 7;
    		["Weapons"] = {"Sword", "Shield"};
    		["Speed"] = 15;
    	};
    	["Dwarf"] = {
    		["Name"] = "Dwarf";
    		["Health"] = 92;
    		["Defense"] = 5;
    		["Weapons"] = {"Hammer", "Axe"};
    		["Speed"] = 20;
    	};
    }
    

    Now that our dictionary is created, we could make a GUI with all the class options. Create a GUI, inside that GUI put a frame, and then create three TextButtons for each class. For the Text property of each text button put the name of one of the classes. This code would detect which button is pressed:

    (REMEMBER: This is a local script!)

    local plr = game.Players.LocalPlayer
    local classChosen
    classes = {
    	["Mage"] = {
    		["Name"] = "Mage";
    		["Health"] = 85;
    		["Defense"] = 10;
    		["Weapons"] = {"Fire Spell", "Freeze Spell"};
    		["Speed"] = 17;
    	};
    	["Warrior"] = {
    		["Name"] = "Warrior";
    		["Health"] = 100;
    		["Defense"] = 7;
    		["Weapons"] = {"Sword", "Shield"};
    		["Speed"] = 15;
    	};
    	["Dwarf"] = {
    		["Name"] = "Dwarf";
    		["Health"] = 92;
    		["Defense"] = 5;
    		["Weapons"] = {"Hammer", "Axe"};
    		["Speed"] = 20;
    	};
    }
    
    script.Parent.Class1.MouseButton1Click:connect(function()
    	for _,v in pairs(classes) do
    		if v["Name"] == script.Parent.Class1.Text then
    			classChosen = v
    			game.ReplicatedStorage.Events.Class:FireServer(classChosen) -- Talked about later on.
    		end
    	end
    end)
    script.Parent.Class2.MouseButton1Click:connect(function()
    	for _,v in pairs(classes) do
    		if v["Name"] == script.Parent.Class2.Text then
    			classChosen = v
    			game.ReplicatedStorage.Events.Class:FireServer(classChosen) -- Talked about later on.
    		end
    	end
    end)
    script.Parent.Class3.MouseButton1Click:connect(function()
    	for _,v in pairs(classes) do
    		if v["Name"] == script.Parent.Class3.Text then
    			classChosen = v
    			game.ReplicatedStorage.Events.Class:FireServer(classChosen) -- Talked about later on.
    		end
    	end
    end)
    

    Now, we should create a folder inside of ReplicatedStorage and call it Events. Inside of that folder we should put a RemoteEvent and call it Class. Now, here's the code for that event:

    (REMEMBER: This is a server script and is located inside of the Class event!)

    script.Parent.OnServerEvent:connect(function(plr, classChosen)
    	local weapons = classChosen["Weapons"]
    	local name      = classChosen["Name"]
    	local health     = classChosen["Health"]
    	local defense  = classChosen["Defense"]
    	local speed     = classChosen["Speed"]
    	
            plr.Character.Humanoid.WalkSpeed = speed
            plr.Character.MaxHealth = health
            plr.Character.Health = health
            -- You might find something to do with the name variable so that is why I implemented it here. A good idea is to create a BillboardGui, and put it above the players head. This would display their health, username, and class.
            -- Also, you should create another RemoteEvent in which when the player is attacked with a weapon it takes that weapons damage and divides it by the defense. For example, if the player is attacked by a Sword that deals 15 damage, and is currently the dwarf class it would run an equation like this: dmg = dmg / defense; plr:TakeDamage(dmg). In this case the player would take 3.2 damage since it's dmg = 16 / 5; plr:TakeDamage(dmg).
    end)
    

    I hope I helped some of you get an idea of where to start creating an RPG. I may put this into action someday, and I hope you would try creating an RPG using these methods.


  • Actually rather neat.

    Are you going to be using a Module Script for the classes instead? It would make things easier to manage in the future.


  • Not that i'm looking to make an RPG , but wouldn't it best to explain what everything does you just say what they are for like dictionaries and stuff. If the person wanted to edited it to his own liking if he could understand it then it would be easier to script so take it into consideration.


  • I made this with the intention of the person using it having a bit of scripting knowledge. Although, I'll take it into consideration.


  • First of all, you should make it clear that the client table classes shouldn't be trusted on the server. The server should have its own table with the trusted values. Also, most of your client code is useless and can be shortened to:

    local Class = game.ReplicatedStorage.Events.Class
    local Buttons = script.Parent:GetChildren()
    
    for a = 1, #Buttons do
    	local Button = Buttons[a]
    	if Button:IsA("GuiButton") then
    		-- Register the Button to a function
    		Button.MouseButton1Click:Connect(function()
    			Class:FireServer(Button.Text)
    		end)
    	end
    end
    

    You should probably state that the classes table should be stored in a ModuleScript

Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.