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!
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.
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)