I’m encountering an issue when try to destroy the cloned frame (created from the server) on the client side. Output show me this below, What could be wrong with my code?
project organization:
server-side script:
local clonedTemplateEvent = game:GetService("ReplicatedStorage").orderTemplate:WaitForChild("clonedTemplateEvent")
clonedTemplateEvent.OnServerEvent:Connect(function(player, username, item1, item2, totalPrice, newFormatText)
local frameTemplate = game:GetService("ReplicatedStorage").orderTemplate.Template
-- Format the text for the textLabel
local formattedText
if item1 and item2 and item2 ~= "" then
formattedText = "Order by " .. username .. ": " .. item1 .. " and " .. item2 .. ". Your earnings: $" .. totalPrice .. " CB"
elseif item1 then
formattedText = "Order by " .. username .. ": " .. item1 .. ". Your earnings: $" .. totalPrice .. " CB"
else
warn("No items provided for the order.")
return
end
-- Loop through all players
for _, plr in ipairs(game.Players:GetPlayers()) do
if plr.Team and plr.Team.Name == "Chef" then
-- Clone the frameTemplate into the ScrollingFrame
local OrdersList_UI = plr:WaitForChild("PlayerGui"):FindFirstChild("OrdersList_UI")
OrdersList_UI.Enabled = true
local clonedFrame = frameTemplate:Clone()
clonedFrame.Parent = OrdersList_UI.Background.ScrollingFrame
-- Update the textLabel with formatted text
clonedFrame.TextLabel.Text = formattedText
else
warn("Player is not in Chef Team.")
end
end
-- Assign the newFormatText variable to formattedText before firing to clients
newFormatText = formattedText
-- Fire the clonedTemplateEvent to the client with the newFormatText
clonedTemplateEvent:FireAllClients(player, newFormatText)
end)
localscript (when press deliver button):
local orderUpdateEvent = game:GetService("ReplicatedStorage").remoteEvents:WaitForChild("OrderUpdateEvent")
local orders = {}
-- Listen to the remote event to receive order update notifications
orderUpdateEvent.OnClientEvent:Connect(function(orderData)
-- Update the client's UI with the new order data
table.insert(orders, orderData) -- Add the order to the local orders table
end)
local function removeOrderAndUI(orderToRemove)
if not orderToRemove then
warn("Order data is nil.")
return
end
-- Check if the order data has the necessary properties
if not orderToRemove.username or not orderToRemove.formattedText then
warn("Invalid order data structure. Missing properties.")
return
end
print(orderToRemove.formattedText)
-- Get the local player and their PlayerGui
local player = game.Players.LocalPlayer
local playerGui = player:FindFirstChild("PlayerGui")
if playerGui then
local OrdersList_UI = playerGui:FindFirstChild("OrdersList_UI")
if OrdersList_UI then
local scrollingFrame = OrdersList_UI.Background:FindFirstChild("ScrollingFrame")
if scrollingFrame then
-- Find and remove the TextLabel associated with the order from the UI
for _, frame in ipairs(scrollingFrame:GetChildren()) do
local textLabel = frame:FindFirstChild("TextLabel")
if textLabel then
if textLabel.Text == orderToRemove.formattedText then
frame:Destroy()
print("TextLabel destroyed successfully.")
break
end
else
warn("TextLabel not found in frame:", frame.Name)
end
end
-- Remove the delivered order from the orders table
for i, order in ipairs(orders) do
if order == orderToRemove then
table.remove(orders, i)
print("Order removed from orders table.")
break
end
end
-- Print a message indicating successful removal
print("Order successfully removed from orders table and UI.")
else
warn("ScrollingFrame not found.")
end
else
warn("OrdersList_UI not found.")
end
else
warn("PlayerGui not found.")
end
end
-- Function to check nearby customer
local function checkNearbyCustomer()
-- Check if there is a customer nearby and return their username if found
local maxDistance = 15 -- Maximum distance to consider a player as nearby (adjust as needed)
local player = game.Players.LocalPlayer
local nearbyCustomer = ""
for _, otherPlayer in ipairs(game.Players:GetPlayers()) do
if otherPlayer ~= player and otherPlayer.Team.Name == "Customer" then
local character = otherPlayer.Character
if character and character:FindFirstChild("HumanoidRootPart") then
local distance = (character.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).magnitude
if distance <= maxDistance then
nearbyCustomer = otherPlayer.Name -- Set the nearby customer username
break
end
end
end
end
return nearbyCustomer
end
-- Function to check if the order exists in the orders table
local function checkOrderExists(username)
-- Check if an order exists for a given username
print("Checking if order exists for username:", username)
for _, order in pairs(orders) do
if order.username == username then
print("Order found for username:", username)
return true -- Order found
end
end
print("Order not found for username:", username)
return false -- Order not found
end
local deliverOrderFunction = game:GetService("ReplicatedStorage").remoteFunctions.deliverOrderFunction
local orderButton = script.Parent
orderButton.MouseButton1Click:Connect(function(player)
print("Delivery order button pressed")
print(orders)
-- Obtain the name of the nearby user
local nearbyCustomer = checkNearbyCustomer()
if nearbyCustomer == "" then
print("No nearby customer found.")
return
end
-- Check if there is an order for the nearby user
local orderExistsForCustomer = checkOrderExists(nearbyCustomer)
if orderExistsForCustomer then
-- Get the order for the nearby user
local orderData = nil
local index = nil
for i, order in ipairs(orders) do
if order.username == nearbyCustomer then
orderData = order
index = i -- Store the index
break
end
end
if orderData then
-- Print the order data before processing
print("Order data before delivery:")
for key, value in pairs(orderData) do
print(key, value)
end
-- Check if orderData is valid before invoking deliverOrderFunction
if not orderData.username or not orderData.item1 or not orderData.item2 or not orderData.totalprice then
warn("Invalid order data")
return
end
-- Call deliverOrderFunction with orderData and index
print("Calling deliverOrderFunction with orderData and index:", orderData, index)
if orderData and index then
-- Get the RemoteFunction for delivering orders from the client
local success, errorMessage = deliverOrderFunction:InvokeServer(orderData)
if success then
print("Order delivery successful.")
-- Update any UI or data related to the removed order here
local OrderReceivedByCustomer_UI = game.Players.LocalPlayer.PlayerGui:WaitForChild("OrderReceivedByCustomer_UI")
OrderReceivedByCustomer_UI.Enabled = true
OrderReceivedByCustomer_UI.ReceivedByCustomer_TextLabel.Text = "Order Received by ".. nearbyCustomer ..". + $".. orderData.totalprice .." CB"
wait(3)
OrderReceivedByCustomer_UI.Enabled = false
wait(.2)
-- Remove the delivered order from orders table and UI
removeOrderAndUI(orderData)
-- Print a message indicating successful removal
print("Order successfully removed from orders table. Index:", index)
else
print("Failed to deliver order:", errorMessage)
end
else
warn("Order data or index is missing or nil")
end
else
print("Failed to find order data for nearby customer:", nearbyCustomer)
end
else
print("No order found for nearby customer:", nearbyCustomer)
end
end)