How to get a player offline using my script which keeps erroring on a stupid statement

local HttpService = game:GetService("HttpService") 

game.ReplicatedStorage.Check.OnServerEvent:Connect(function(UserWhoCalled, UserName)
	local function status(username, id)
		local URL = "https://www.rprxy.xyz/users/"..id.."/profile"
		local user = HttpService:GetAsync(URL, true)

		if (string.find(user, "icon%-game") ~= nil) then
			return "in a game"
		elseif (string.find(user, "icon%-online") ~= nil) then
			return "online"
		else
			return "offline"
		end
			if (string.find(user, "icon%-studio") ~= nil) then
				return "in studio"
			else
				return "offline"
			end
	end

	local URL1 = "https://api.rprxy.xyz/users/get-by-username?username="..UserName -- API that you want to get from
	local URL2 = "http://friends.rprxy.xyz/v1/users/"..game.Players:GetUserIdFromNameAsync(UserName).."/friends/count"
	
	local Response = HttpService:GetAsync(URL1)
	local Data = HttpService:JSONDecode(Response)
	local Response2 = HttpService:GetAsync(URL2)
	local Data2 = HttpService:JSONDecode(Response2)
	
	script.Parent.Online.Text = UserName.." is "..status(UserName, game.Players:GetUserIdFromNameAsync(UserName))
	
	script.Parent.ID.Text = "Player ID: "..Data.Id
	script.Parent.Username.Text = "Player Username: "..Data.Username
	
	if Data2.count > 1 then
		script.Parent.FriendCount.Text = UserName.." has "..Data2.count.." friends."
	elseif Data2.count == 0 then
		script.Parent.FriendCount.Text = UserName.." has no friends."
	end
	
	if Data2.count == 1 then
		script.Parent.FriendCount.Text = UserName.." has "..Data2.count.." friend."
	end
end)

You do not know how annoying this script has been to me.

I hope that makes sense because I am really stressed because of this script.

As you can see, I am making it return online, offline, in studio or in game BUT THIS STUPID SCRIPT SAYS THIS ERROR: image

How can I make it so it stops erroring and shows the player is offline is they are offline?

Somebody please help me, I am litterly feeling like kicking my PC off a cliff right now. Thanks for liking @Liker1

1 Like

You missed out an end) statement here:

game.ReplicatedStorage.Check.OnServerEvent:Connect(function(UserWhoCalled, UserName)
	local function status(username, id)
		local URL = "https://www.rprxy.xyz/users/"..id.."/profile"
		local user = HttpService:GetAsync(URL, true)

		if (string.find(user, "icon%-game") ~= nil) then
			return "in a game"
		elseif (string.find(user, "icon%-online") ~= nil) then
			return "online"
		else
			return "offline"
		end
			if (string.find(user, "icon%-studio") ~= nil) then
				return "in studio"
			else
				return "offline"
			end
	end
end) --Here, to close the OnServerEvent function.

No I don’t want to close the on server event function.

You need to read the script I gave in the post. Go read it because I have more stuff below the function.

I have this now…

local HttpService = game:GetService("HttpService") 

game.ReplicatedStorage.Check.OnServerEvent:Connect(function(UserWhoCalled, UserName)
	local function status(username, id)
		local URL = "https://www.rprxy.xyz/users/"..id.."/profile"
		local user = HttpService:GetAsync(URL, true)

		if (string.find(user, "icon%-game") ~= nil) then
			return "in a game"
		elseif (string.find(user, "icon%-online") ~= nil) then
			return "online"
		else
			return "offline"
		end
			if (string.find(user, "icon%-studio") ~= nil) then
				return "in studio"
			else
				return "offline"
			end
	end
end) --Here, to close the OnServerEvent function.

	local URL1 = "https://api.rprxy.xyz/users/get-by-username?username="..UserName -- API that you want to get from
	local URL2 = "http://friends.rprxy.xyz/v1/users/"..game.Players:GetUserIdFromNameAsync(UserName).."/friends/count"
	
	local Response = HttpService:GetAsync(URL1)
	local Data = HttpService:JSONDecode(Response)
	local Response2 = HttpService:GetAsync(URL2)
	local Data2 = HttpService:JSONDecode(Response2)
	
	script.Parent.Online.Text = UserName.." is "..status(UserName, game.Players:GetUserIdFromNameAsync(UserName))
	
	script.Parent.ID.Text = "Player ID: "..Data.Id
	script.Parent.Username.Text = "Player Username: "..Data.Username
	
	if Data2.count > 1 then
		script.Parent.FriendCount.Text = UserName.." has "..Data2.count.." friends."
	elseif Data2.count == 0 then
		script.Parent.FriendCount.Text = UserName.." has no friends."
	end
	
	if Data2.count == 1 then
		script.Parent.FriendCount.Text = UserName.." has "..Data2.count.." friend."
	end
end)

Which errors on the last line of the WHOLE SCRIPT

Oh my bad, ignore my first post. I think I found it:

	local function status(username, id)
		local URL = "https://www.rprxy.xyz/users/"..id.."/profile"
		local user = HttpService:GetAsync(URL, true)

		if (string.find(user, "icon%-game") ~= nil) then
			return "in a game"
		elseif (string.find(user, "icon%-online") ~= nil) then
			return "online"
		else
			return "offline"
		end

In this part, the function would be returned no matter what the outcome is. If they are in a game, it would return. If they are online, it would return. If they are offline, it would return. Meaning that the code below it would never be executed. You can solve this by using a variable instead:

game.ReplicatedStorage.Check.OnServerEvent:Connect(function(UserWhoCalled, UserName)
    local playerStatus --Your new variable.
	local function status(username, id)
		local URL = "https://www.rprxy.xyz/users/"..id.."/profile"
		local user = HttpService:GetAsync(URL, true)

		if (string.find(user, "icon%-game") ~= nil) then
			playerStatus = "in a game"
		elseif (string.find(user, "icon%-online") ~= nil) then
			playerStatus = "online"
		else
			playerStatus = "offline"
		end
			if (string.find(user, "icon%-studio") ~= nil) then
				playerStatus  = "in studio"
			else
				playerStatus  = "offline"
			end
	end
--The rest of the code.

I think this should be it… sorry for not reading it the first time round haha.

1 Like

Ah thanks!

Also it’s ok for not reading the Original Post. I will try it right now.

Okay, hope it works! Also just wanted to let you know it is because the return statement gives you a value but it also breaks the function. :slightly_smiling_face:

1 Like

Ok its works but… it says the player is offline if they are online… I will try inputting the username again just in case it was an API error,

Yeah no, it works but it says the player is offline when the player is actually online

Even if the player is in a game it says the player is offline

I don’t think that has anything to do with the script’s error then. You should add a print() below them, if it prints offline too then it would probably be a mistake in retrieving information from the API. That would not be an API error but perhaps you can retrieve it differently? I haven’t used APIs in awhile so I will see what I can find. :man_shrugging:

I think I see why but I don’t know how to fix this.

Your script asks if the player is in a game or or online but somehow it says it’s offline

Oh ok, let me see.

Yeah it prints offline still… check your script because I kinda see where u went wrong.

None of it is my script, it’s all yours. I only fixed the error which you requested to be fixed. I did not touch any of the HTTP requests - but I am pretty bored so I will experiment around with it too and tell you what I can get.

Again, none of it is my script. The only thing I changed was your return error.

Ok, I will upload my GUI as a model and I will send u the link in ur DM’s for u to use

Yep I mentioned it here, but we don’t need to know what is wrong. We just need to know what can be done to fix it.

Hang on, let me try and look at ur script.

Thanks to @MrOofBoyPlayz for sharing the model with me in DMs! The problem has been resolved, but just in case anybody runs into this in the future, I will share it here too.

	local function status(username, id)
		local URL = "https://www.rprxy.xyz/users/"..id.."/profile"
		local user = HttpService:GetAsync(URL, true)

		if (string.find(user, "icon%-game") ~= nil) then
			playerStatus = "in a game"
		elseif (string.find(user, "icon%-online") ~= nil) then
			playerStatus = "online"
		else
			playerStatus = "offline"
		end
			if (string.find(user, "icon%-studio") ~= nil) then
				playerStatus  = "in studio"
			else
				playerStatus  = "offline"
			end
	end

In this function, the if statement at the end would check if the player is in Studio. If they are not, it would always revert to offline. It can be fixed by running through all of the possible statuses in a desired order:

	local function status(username, id)
		local URL = "https://www.rprxy.xyz/users/"..id.."/profile"
		local user = HttpService:GetAsync(URL, true)

		if (string.find(user, "icon%-game") ~= nil) then
			playerStatus = "in a game"
		elseif (string.find(user, "icon%-online") ~= nil) then
			playerStatus = "online"
		elseif (string.find(user, "icon%-studio") ~= nil) then
			playerStatus = "in Studio"
		else
			playerStatus = "offline"
		end
	end

And intially, the function would be returned no matter what the outcome is. If they are in a game, it would return. If they are online, it would return. If they are offline, it would return. Meaning that the code below it would never be executed. :slightly_smiling_face:

2 Likes