Fixing getting 'nil' from a returned string

Hi, developers!

I’m aware that there are posts like this, but the situation I have is a little different.

I’m trying to code a Code system, where players can redeem codes for UGC. I’ve made it so far where it’ll work if the code isn’t expired, but my problem comes when returning my string.

local uis = game:GetService('UserInputService')
local textBox = script.Parent
local main = textBox.Parent

textBox.FocusLost:Connect(function(enterPressed)
	if enterPressed == true then
		textBox.TextEditable = false
		
		local message = game.ReplicatedStorage.SubmitCode:FireServer(textBox.Text)
		textBox.Text = tostring(message)
		task.wait(2)
		textBox.Text = ""
		textBox.TextEditable = true
	end
end)

This should give me, if I type in a valid code, “Redeemed code successfully!”, but instead gives me “nil”. I could remove the first if statement, but that would kind of ruin the experience, since pressing enter is how I want players to redeem codes, rather than just any form of losing textfocus.

Any help is greatly appreciated, and any redirection to posts that could help solve this problem would also be greatly appreciated. Thanks!

This is not how fire server work. You will not get any callback from this

The message string is supposed to be a string returned by a server script that handles codes. Heres’ the server script:

local codesList = require(script.Codes)

game.Players.PlayerAdded:Connect(function(player)
	local codesFolder = Instance.new('Folder', player)
	codesFolder.Name = 'Codes'
end)

game.ReplicatedStorage.SubmitCode.OnServerEvent:Connect(function(player, code)
	if codesList[code] ~= nil then
		if player.Codes:FindFirstChild(code) == nil then
			if codesList[code] == true then
				print('awesome code has expired :(')
				return "This code has expired!"
			else
			local temp = Instance.new('BoolValue', player.Codes)
				temp.Name = code
				print('awesome code redeemed successfully')
				return "Redeemed code successfully!"
		end
	end
		return "Invalid code!"
	end
end)

Your code would work by normal means, but it’s not quite what I’m looking for.

This code will return this string to function not remote. You will need new remote event using FireClient() to get string back to player.

1 Like

Or change player text in button using server script

local codesList = require(script.Codes)

game.Players.PlayerAdded:Connect(function(player)
	local codesFolder = Instance.new('Folder', player)
	codesFolder.Name = 'Codes'
end)

game.ReplicatedStorage.SubmitCode.OnServerEvent:Connect(function(player, code)
    local text = ""
	if codesList[code] ~= nil then
		if player.Codes:FindFirstChild(code) == nil then
			if codesList[code] == true then
				print('awesome code has expired :(')
                text = "This code has expired!"
				return "This code has expired!"
			else
			local temp = Instance.new('BoolValue', player.Codes)
				temp.Name = code
				print('awesome code redeemed successfully')
                text = "Redeemed code successfully!"
				return "Redeemed code successfully!"
		end
	end
        text = "Invaild code!"
		return "Invalid code!"
	end
    player.PlayerGui. 'somthing'.textBox.Text = text
end)

Just remember to get right parents of textBox

Or alternatively use remote functions?

Like you need remote function to use FireClient() so yes

No? You can just use return on OnServerInvoke to return anything back to the client without needing remotes?

Hi! I realised that, when coding this, I was supposed to be using a remote functions rather than my remote event. Thanks for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.