I have been trying to create a portal system for my game (I thought it was going to be simple) but because I’m very new to scripting I have an error that I don’t know how to fix. Here is the error:
Players.cookie0609life.PlayerGui.Frames.BuyPortal.Handler:17: attempt to index nil with ‘Name’
And here is the script:
wait()
local INFO = script.Parent.Info
local Title = script.Parent.Title.TextLabel
local Portal = game:WaitForChild("Workspace").MainMap.Portals:GetChildren()
local RS = game:WaitForChild("ReplicatedStorage")
local EVENt = RS.RemoteEvents.PurchaseWorld
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage.RemoteEvents.PurchaseWorld
local price = Portal.price
remoteEvent.OnClientEvent:Connect(function(Portal, price)
wait()
script.Parent.Visible = true
Title.Text = "Buy" .. Portal.Name
script.Parent.Title.CopyTTExt.Text = "Buy" .. Portal.Name
INFO.Text = "Would you like to buy " .. Portal.Name .. " For " .. price.Value
end)
local BuyBUTTON = script.Parent.BuyButton
local CancelBUTTON = script.Parent.CancelButton
if script.Parent.Visible == true then
BuyBUTTON.MouseButton1Down:Connect(function(plr)
if plr.leaderstats.Clicks.Value >= Portal.price.Value then
wait()
plr.leaderstats.Clicks.Value = plr.leaderstats.Clicks.Value - Portal.price.Value
script.Parent.Visible = false
Portal.PortalHitBox.Script.Disable = false
wait()
else
local ErrorText = Instance.new("TextLabel")
ErrorText.Parent = script.Parent.Parent.Parent.Indicators.ErrorPopup
ErrorText.Text = "Error: Not enough Coins"
wait(3)
ErrorText:Destroy()
end
end)
CancelBUTTON.MouseButton1Down:Connect(function ()
script.Parent.Visible = false
local CancelText = Instance.new("TextLabel")
CancelText.Parent = script.Parent.Parent.Parent.Indicators.ErrorPopup
CancelText.Text = "Server: Cancelled Purchase"
wait(3)
CancelText:Destroy()
end)
end
The script opens a GUI popup when a client event is fired.
The error happens on line 17. I’m very new to scripting so sorry if its terrible. But if anyone could help me, I would be very thankful.
How is the server sending Portal to the client? If the Portal Instance only exists on the server (ServerStorage, ServerScriptService) then it will appear nil on the client due to the fact that the client cannot see anything parented to those services.
Actually, you can with RemoteFunctions I think,
Anyways just make sure Portal exists somewhere in workspace, try printing Portal when remoteEvent is fired to the client
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
local RS = game:WaitForChild("ReplicatedStorage")
local remoteEvent = RS.RemoteEvents.PurchaseWorld
remoteEvent:fireClient(plr)
end
end)
Ok, so put in the parameters, but instead of it printing the name of the portal it prints ‘price’
wait()
local INFO = script.Parent.Info
local Title = script.Parent.Title.TextLabel
local Portal = game:WaitForChild("Workspace").MainMap.Portals:GetChildren()
local RS = game:WaitForChild("ReplicatedStorage")
local EVENt = RS.RemoteEvents.PurchaseWorld
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage.RemoteEvents.PurchaseWorld
local price = Portal.price
remoteEvent.OnClientEvent:Connect(function(plr, Portal, price)
wait()
print(Portal)
end)