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

Can someone help me create a text label out of a Http service request?

Asked by 6 years ago
Edited 6 years ago

I am currently creating an Http service system where the data is shown on a table from the API. I was testing my api earlier by making an HTTP request. Everything worked fine but I was wondering if someone could help me put a column in a table. Currently, this is the code I have.

-- Script to test Http Service 

-- Defining variables etc. 

http = game.HttpService

-- Script for when the player joins the game

game.Players.PlayerAdded:Connect(function (plr)

    local data = http:GetAsync("https://./api.php/users/1?columns=name", true)
    print(data)

end)

I am requesting for the name of the user with the id 1.

It outputs:

{"name":"Default Admin"}

Could anyone tell me how I could place this into a text label so it would say Default Admin?

(Removed web address to protect my api)

0
Just a side note. I am planning on doing a system where it sends a request based on the players name. So I WILL be needing to use this however for now I will just be testing so in other words please don't just send me messages saying I don't need to use Http Service.. PureGamerGames 16 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

First off, you need to make your database secure. It's very easy for hackers to get your API web address even if it's in a private module. Make sure people can't edit your database from your URL. Back to LUA though.

You have to create a TextLabel on the client. This can be done many ways but the method I'm about to show you is a little hacky and I suggest you read up about RemoteEvents.

-- SERVICES
local Players= game:GetService("Players")
local HttpService = game:GetService("HttpService")

-- FUNCTIONS
local function playerAdded(player)
    -- Please look up Http request limits and pcall functions, this will error with over use.
    local data = HttpService:GetAsync("https://./api.php/users/1?columns=name", true)

    -- DECODE THE JSON INTO A LUA TABLE (https://wiki.roblox.com/index.php?title=API:Class/HttpService/JSONDecode)
    local luaData = HttpService:JSONDecode(data)

    -- This is just long heap of code to generate a client gui.
    local screenGui = Instance.new("ScreenGui")
    screenGui.Name = "databaseRank"
    screenGui.ResetOnSpawn = false -- Make sure this is false or the gui will wipe after respawn!!!

    -- This create the textLabel that shows the text.
    local rankTag = Instance.new("TextLabel")
    rankTag.Parent = screenGui
    rankTag.BackgroundColor3 = Color3.new(1, 1, 1)
    rankTag.BorderColor3 = Color3.new(1, 1, 1)
    rankTag.BorderSizePixel = 0
    rankTag.Position = UDim2.new(0, 10, 0, 10)
    rankTag.Size = UDim2.new(0, 200, 0, 50)
    rankTag.Font = Enum.Font.SourceSans
    rankTag.TextColor3 = Color3.new(0, 0, 0)
    rankTag.TextSize = 25

    -- Decode JSON table to be read with in Roblox, if you don't 
    -- know what a JSON and a LUA table is then I suggest you shouldn't
    -- be trying to mess around with web api's and stuff.
    rankTag.Text = luaData["name"]
    screenGui.Parent = player:WaitForChild("PlayerGui")

end

Players.PlayerAdded:Connect(playerAdded)

Ideally this would be run on the client side but for debug purposes, this works.

You should find out how RemoteEvents can be used to invoke the client and generate the GUI on the user's machine rather than on the server.

Also, don't just copy and paste my code, read the comments I put it, they are important if you want to improve as a programmer.

0
Thank you. I have modified the code slightly as the GUI is already created as you join the game but apart from that thanks. I read the tips and I will be making my api more secure. PureGamerGames 16 — 6y
Ad

Answer this question