Currently, I am making a trail shop where players can purchase trails for their avatars. I have the shop gui set up so players can click the trail they want to see, preview it on the righthand side, and possibly purchase it. When the player clicks the buy button, it sends a remote event to a regular script in ServerScriptService.
Here is the local script, sending the remote event:
local itemPreview = script.Parent.Parent.ItemPreview
local itemsFolder = game.ReplicatedStorage:WaitForChild("Trails")
itemPreview.TextButton.MouseButton1Click:Connect(function()
game.ReplicatedStorage.TrailSelectedRE:FireServer(itemsFolder[itemPreview.Title.Text])
end)
Then, here is the script in serverscriptservice:
game.ReplicatedStorage.TrailSelectedRE.OnServerEvent:Connect(function(plr, buying, itemBought)
print("Message Recieved")
if buying and not plr.OwnedTrails:FindFirstChild(itemBought.Name) then
local price = itemBought.Price.Value
local coins = plr.leaderstats.Coins
if price <= coins.Value then
coins.Value -= price
itemBought:Clone().Parent = plr.OwnedTrails
end
elseif not buying and plr.OwnedTrails:FindFirstChild(itemBought.Name) then
local char = plr.Character
if not char or not char:FindFirstChild("HumanoidRootPart") then return end
for i, child in pairs(char.HumanoidRootPart:GetChildren()) do
if child:IsA("Trail") then child:Destroy() end
end
local newTrail = itemBought:Clone()
newTrail.Attachment0 = char.HumanoidRootPart.TrailTop
newTrail.Attachment1 = char.HumanoidRootPart.TrailBottom
newTrail.Parent = char.HumanoidRootPart
end
end)
The issue is that I am getting an error on this line:
if buying and not plr.OwnedTrails:FindFirstChild(itemBought.Name) then
saying attempt to index nil with ‘Name’ error. What does this mean and how do I fix it? Note: There are parts of the script I did not include for both of the scripts so if you would like to see them, just ask. Also, if I didn’t explain anything thoroughly or if you want more of an explanation, just ask. Also, if you see any inconsistencies or problems that might occur in the future with my script, please communicate them with me.
Yes, I just noticed it too. Thats a big mistake on my part. I want the item selected for the preview be sent to the server script to say that that item should be bought. I define the item selected earlier in the local script that I did not include.
function updateShop ()
for i, item in pairs(itemsFolder:GetChildren()) do
local name = item.Name
local price = item.Price.Value
local desc = item.ItemDescription.Value
local imagepreview = item.Folder.ImageLabel
local itemSelection = script.ItemSelection:Clone()
itemSelection.Parent = itemScroller
itemSelection.MouseButton1Click:Connect(function()
itemPreview.Title.Text = name
itemPreview.TextButton.Text = "Buy for " .. price
itemPreview.Desc.Text = desc
itemPreview.PreviewImage.Image = imagepreview.Image
itemPreview.Visible = true
end)
end
end
itemPreview.TextButton.MouseButton1Click:Connect(function()
game.ReplicatedStorage.TrailSelectedRE:FireServer(itemsFolder[itemPreview.Title.Text], itemSelection)
end)
How would I go about making sending the selected item defined by the local script to the serverscript and how should I incorporate it there?
I want the item selected for the preview to be sent to the server script so that that specific item should be bought. I define the item selected in the preview earlier in the local script that I didn’t include. (Look at the script I included for the previous person). How should I incorporate the selected item for the preview to be sent to the server script so that that item is bought?
I have not made any changes to the script. I want to fix it so its not itemBought, but something pertaining to what I had defined in the local script earlier like itemSelection. Summarizing, I want it to send information over to the script in serverscriptservice so that when the buy button is clicked, the item selected that is shown in the preview is the one bought.
I have tried multiple things but none of them have worked. I am quite confused on how to send the previewed item to the server script telling it that that item should be bought.