Whats wrong with my script?

When I play my game I get an error saying “ServerScriptService.Script:10: attempt to index nil with ‘PlayerGui’” when I write an asset id into a text box.

Here is my script:

wait(0.1)

local plr = game:GetService("Players").LocalPlayer

local Mps = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("NewID")

local function newID()
	local id = plr.PlayerGui.Main.MainFrame.EnterID.Text
	local asset = Mps:GetProductInfo(id)

	print(asset.Name .. " :: " .. asset.Description)
end


remoteEvent.OnServerEvent:Connect(newID)

The local script that I use to trigger the remote event:

script.Parent.FocusLost:Connect(function(plr)
print(script.Parent.Text)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("NewID")

remoteEvent:FireServer()

end)

Whats wrong with my script? Thanks for your time!

server scripts cannot get the player lemme rewrite


wait(0.1)

local Mps = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("NewID")

local function newID(plr)
	local id = plr:WaitForChild("PlayerGui").Main.MainFrame.EnterID.Text
	local asset = Mps:GetProductInfo(id)

	print(asset.Name .. " :: " .. asset.Description)
end


remoteEvent.OnServerEvent:Connect(newID)

events send the plr since server cannot thats how we communicate

plr is nil. There is no LocalPlayer on the server; you can only access that in LocalScripts.

This almost works but now I get new error:
Screen Shot 2021-05-22 at 2.39.38 PM

Im not sure why

Servers won’t be able to see what is written in the box. So it is better to send the text through the client.

The local script,

script.Parent.FocusLost:Connect(function(plr)
    print(script.Parent.Text)
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local remoteEvent = ReplicatedStorage:WaitForChild("NewID")

    remoteEvent:FireServer(script.Parent.Text)
end)

And the server script,

local function newID(plr, text)
    local asset = Mps:GetProductInfo(text)
    print(asset.Name .. " :: " .. asset.Description)
end

remoteEvent.OnServerEvent:Connect(newID)
2 Likes

Thank you so much, this works perfectly

Is there a way that I can change the text of a textlabel to the asset’s name?

1 Like

You could reference the label in the server script.

game:GetService("Workspace").TextLabel.Text = asset.Name -- Example
1 Like

I’m not very good with scripting, but I tried doing this:

plr.PlayerGui.Main.MainFrame.Name.Text = asset.Name

and I received and error saying “ServerScriptService.Script:8: attempt to index string with ‘Text’”

Name is a property. Change the Name’s name and try again.

plr.PlayerGui.Main.MainFrame.PlayerName.Text = asset.Name 

Something like this.

Oh, I can’t believe I forgot about that, tysm!

1 Like