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

why wont the server find the player?

Asked by 4 years ago

hi hello i am making a game

this is the part where i tell you to forcefully fix my code so here it is:

CLIENT:

--#FUNCTIONS
function checkProfile(playerName)

    local xpLVL, description, roundsCompleted = game.ReplicatedStorage.Remotes.Profile.CheckPlayerStats:FireServer(playerName)

    script.Parent.profileFrame.XPlevel.Text = "experience level: "..xpLVL
    script.Parent.profileFrame.description.Text = description
    script.Parent.profileFrame.roundsCompleted.Text = "rounds completed: "..roundsCompleted

end

script.Parent.titlescreen.profile.MouseButton1Click:Connect(function()

    checkProfile(game.Players.LocalPlayer.Name)

end)

SERVER:

game.ReplicatedStorage.Remotes.Profile.CheckPlayerStats.OnServerEvent:Connect(function(playerName)

    local player = game.Players:GetPlayerFromCharacter(playerName)

    if player then

        if player:FindFirstChild("xpLVL") and player:FindFirstChild("desc") and player:FindFirstChild("keyboard") and player:FindFirstChild("roundsCompleted") then

            return player.xpLVL.Value, player.desc.Value, player.roundsCompleted.Value

        else

            print(">:(")

        end

    else

        print("[P,SD[GQERWOP[]]GFOQREP[]GM,QAERP[OGMQO[IEWPRGMIQWOPRmfgviopwmRIWGORPmgoip")

    end

end)

the problem is that it keeps returning the player stats as nil. for some reason the server wont find the player. i've tried this multiple times before in my other games, and it worked. can y'all please tell me why the error keeps happening? thanks :)

server error: ServerScriptService.Remotes:18: invalid argument #2(string expected, got Instance) client error: Players.L_0ID.PlayerGui.mainGui.main.Core:37: attempt to concatenate string with nil

0
you can make it easier by find first child in game.players studys1 0 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

EDIT: It seems like you're using RemoteEvent to fetch the player profile. Consider using RemoteFunction instead! Sorry for slipping through that!

TL;DR: Use RemoteFunction instead, and the parameters in the Server script is incorrect. Instead of just (playerName), it should be (plr, playerName), since the first parameter of OnServerEvent/OnServerInvoke is always the player client that sent this request to the server.

okay first:

The first parameter of OnServerEvent and OnServerInvoke (from RemoteFunction class) is by default, the Player object that tells you the client that the RemoteFunction is fired from. Check this

This brings us to:

--SERVER SCRIPT (To illustrate what you should do)
--We should use RemoteFunction here if you want the results to be passed to the client.
--Bind the function to the RemoteFunction
game.ReplicatedStorage.Remotes.Profile.CheckPlayerStats.OnServerInvoke = function(plr, playerName)
    --plr will return the player client that fired this event, while playerName will be passed as the first parameter that the client has fired from.
    --Rest of your script

    --Return your results after some scavenging
    if found then
        return result, blabla, anything
    end

    --Don't forget to put a return here in case if the condition above is not true
    return nil
end)

Since we're using RemoteFunction for this task, from the Client script, instead of calling :FireServer()method, use :InvokeServer() instead. They're the same for communication through Server and Client, but there is a difference. Here's a comprehensive documentation of those two, which explains how these function in case you're interested!

Second: From the Server script I read, you don't have to use GetPlayerFromCharacter, especially in this case where you're passing just the player's name only, not the player's Character (which is a Model, not a string, not written in text)

game.Players contains all the Players instances you need, which also includes you and the person you want to check. We'll apply this fix:

--Replace this
local player = game.Players:GetPlayerFromCharacter(playerName)
--To this:
local player = game.Players:FindFirstChild(playerName)

I'm on mobile right now, so these are untested... If anything, please let me know about any faults I made or if this problem persists. cheers c:

0
also I don't know why this worked in your other games... i might've missed something from that Afterl1ght 321 — 4y
Ad

Answer this question