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()
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
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)