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

Attempt to index nil with ''UserId"?

Asked by 4 years ago

I've been creating a Auto-Rank Application Center using this tutorial: https://devforum.roblox.com/t/how-to-create-a-rank-management-system-using-glitch/442725

I have been able to get the GUI working, which if a player gets >= 9 for their score, it fires the server, which I have set up a remote event in replicated storage which fires when the Event is fired. Everything is working, but when the event is run, I get the error "ServerScriptService.Main:9: attempt to index nil with 'UserId'"

Scripts Below

Main Script

game.ReplicatedStorage.SetRank.OnServerEvent:Connect(function()
    local GroupId = 6151289
    local HttpService = game:GetService("HttpService")
    local Server = require(script.Server)
    local domain = "removed for privacy reasons"
    local key = "removed for privacy reasons"
    local player = game.Players.LocalPlayer

    if player.UserId == player.UserId then
        Server.SetRank(GroupId, player.UserId, 329)
    end
end)

Server Script

local Server = {}

local HttpService = game:GetService("HttpService")

local Configs = require(script.Parent.Configs)


local function Request(Function, RequestBody)

    --Before sending the request, add our auth_key to the body
    RequestBody["auth_key"] = Configs.AUTH_KEY

    local response = HttpService:RequestAsync(
        {
            -- The website to send the request to. Function is the extended part of the URL for specific functions.
            -- In this case, Function = 'GroupShout'
            -- Example: 
            --  "Configs.BaseUrl..Function" would be equal to: http://test-app.glitch.me/GroupShout

            Url = Configs.BaseUrl..Function, 

            -- The request method (all of ours will be POST)
            Method = "POST",

            -- We are sending JSON data in the body
            Headers = {
                ["Content-Type"] = "application/json"
            },
            -- The body of the request containing the parameters for the request
            Body = HttpService:JSONEncode(RequestBody)
        }
    )

    if response.Success then
        print("Status code:", response.StatusCode, response.Body)
        print("Response body:\n", response.Body)

        return response.Body
    else
        print("The request failed:", response.StatusCode, response.Body)
        return response.Body
    end
end

Server.Promote = function(GroupId, UserId)
    assert(typeof(GroupId) == "number", "Error: GroupId must be an integer") -- Throw error if GroupId is not an integer
    assert(typeof(UserId) == "number", "Error: UserId must be an integer") -- Throw error if UserId is not an integer

    local Body = {
        Group = GroupId;
        Target = UserId;
    }

     -- pcall the function 'Request', with arguments 'Promote' and Body
    local Success, Result = pcall(function()
        return Request('Promote', Body)
    end)

    print(Result)
end

Server.Demote = function(GroupId, UserId)
    assert(typeof(GroupId) == "number", "Error: GroupId must be an integer") -- Throw error if GroupId is not an integer
    assert(typeof(UserId) == "number", "Error: UserId must be an integer") -- Throw error if UserId is not an integer

    local Body = {
        Group = GroupId;
        Target = UserId;
    }

    local Success, Result = pcall(function()
        return Request('Demote', Body)
    end)

    print(Result)
end

Server.SetRank = function(GroupId, UserId, RankId)
    assert(typeof(GroupId) == "number", "Error: GroupId must be an integer") -- Throw error if GroupId is not an integer
    assert(typeof(UserId) == "number", "Error: UserId must be an integer") -- Throw error if UserId is not an integer
    assert(typeof(RankId) == "number", "Error: RankId must be an integer") -- Throw error if RankId is not an integer

    local Body = {
        Group = GroupId;
        Target = UserId;
        Rank = RankId;
    }

    local Success, Result = pcall(function()
        return Request('SetRank', Body)
    end)

    print(Result)
end

Server.HandleJoinRequest = function(GroupId, PlayerUsername, Boolean)
    assert(typeof(GroupId) == "number", "Error: GroupId must be an integer") -- Throw error if GroupId is not an integer
    assert(typeof(PlayerUsername) == "string", "Error: PlayerUsername must be a string") -- Throw error if PlayerUsername is not a string
    assert(typeof(Boolean) == "boolean", "Error: Boolean must be a boolean value") -- Throw error if Boolean is not a boolean value

    local Body = {
        Group = GroupId;
        Username = PlayerUsername;
        Accept = Boolean; -- true or false
    }

    local Success, Result = pcall(function()
        return Request('HandleJoinRequest', Body)
    end)

    print(Result)
end

Server.GroupShout = function(GroupId, ShoutMessage)
    assert(typeof(GroupId) == "number", "Error: GroupId must be an integer") -- Throw error if GroupId is not an integer
    assert(typeof(ShoutMessage) == "string", "Error: ShoutMessage must be a string") -- Throw error if ShoutMessage is not a string

    local Body = {
        Group = GroupId;
        Message = ShoutMessage;
    }

    local Success, Result = pcall(function()
        return Request('GroupShout', Body)
    end)

    print(Result)
end

return Server

2 answers

Log in to vote
1
Answered by 4 years ago

Hello, tylerisyummy! The problem is that LocalPlayer returns nil in a regular/server script as the server doesn't run on clients. Instead, use the automatic "player" parameter in the OnServerEvent event. Change your main script to this:

game.ReplicatedStorage.SetRank.OnServerEvent:Connect(function(player)
    local GroupId = 6151289
    local HttpService = game:GetService("HttpService")
    local Server = require(script.Server)
    local domain = "removed for privacy reasons"
    local key = "removed for privacy reasons"

    if player.UserId == player.UserId then
        Server.SetRank(GroupId, player.UserId, 329)
    end
end)

Please accept this answer and upvote it if it helped you.

Ad
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

The reason maybe that you didn't concatenate two strings together like so do Make sure it's in a server script.


if player == "player" ..player.UserId then Server.SetRank(GroupId, player.UserId, 329) end end)
0
This answer is incorrect, just follow the above answer, as the answer should be correct! JesseSong 3916 — 3y

Answer this question