-
I want to display items within a gui which users can click on it then deducts the money from their Money in leaderstats and then closes the GUi and allows the users to place these items anywhere on their plots.
-
The Blocks do not show in the gui, they dont show for the plot. see video.
robloxapp-20230922-1524268.wmv (435.1 KB)
here is a couple of screen shots to show where things are:
PlacementGUI script:
-- Client-Side Script: PlacementGuiScript
print("Script started")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local user = Players.LocalPlayer
local placementEvent = ReplicatedStorage:WaitForChild("PlacementEvent")
local getPlotFunction = ReplicatedStorage:WaitForChild("GetPlotFunction") -- Add this line
local leaderstats = user:WaitForChild("leaderstats") -- Wait for leaderstats to be available
local moneyValue = leaderstats:WaitForChild("Money") -- Wait for Money to be available
local getSetMoney = ReplicatedStorage:WaitForChild("GetSetMoney")
-- Function to handle leaderboard setup
local function handleLeaderboardSetup()
print("Leaderboard already set. Nothing to do here.")
end
local leaderboardSetupEvent = ReplicatedStorage:WaitForChild("LeaderboardSetupEvent", 5)
if leaderboardSetupEvent then
leaderboardSetupEvent.OnClientEvent:Connect(handleLeaderboardSetup)
else
print("Failed to find leaderboardSetupEvent")
end
local buildConfigs = script.Parent
local blocksShowcase = buildConfigs:WaitForChild("BlocksShowcase")
local scrollFrame = blocksShowcase:WaitForChild("ScrollFrame")
local openGuiButton = buildConfigs:WaitForChild("OpenGui")
local closeButton = blocksShowcase:FindFirstChild("Close")
local placementEvent = ReplicatedStorage:WaitForChild("PlacementEvent")
local selectedBlock = nil
-- Function to place a block on a plot
local function placeBlock()
print("Attempting to place block...")
local currentMoney = getSetMoney:InvokeServer("get")
local plotName = getPlotFunction:InvokeServer() -- Add this line
if selectedBlock and currentMoney and plotName then -- Modify this line
local cost = selectedBlock:FindFirstChild("Cost")
if cost and currentMoney >= cost.Value then
print("Placing block...")
getSetMoney:InvokeServer("set", currentMoney - cost.Value)
placementEvent:FireServer(selectedBlock.Name, plotName) -- Modify this line
else
print("Not enough money or invalid block")
end
else
print("No block selected, money not found or plot not owned")
end
end
-- Function to toggle BlocksShowcase GUI visibility
local function toggleGui()
blocksShowcase.Visible = not blocksShowcase.Visible
end
-- Function to close BlocksShowcase GUI
local function closeGui()
blocksShowcase.Visible = false
end
-- Populating blocks
for _, category in pairs({"Builds", "Decoration", "Interior"}) do
local categoryFolder = ReplicatedStorage:FindFirstChild("Builder"):FindFirstChild(category)
if categoryFolder then
for _, block in pairs(categoryFolder:GetChildren()) do
if block:IsA("Model") then
local cost = block:FindFirstChild("Cost")
if cost then
local blockButton = Instance.new("TextButton")
blockButton.Size = UDim2.new(0, 128, 0, 128)
blockButton.BackgroundColor3 = Color3.new(1, 1, 1)
blockButton.Text = ""
blockButton.Parent = scrollFrame
local viewport = Instance.new("ViewportFrame")
viewport.Parent = blockButton
local camera = Instance.new("Camera")
camera.Parent = viewport
viewport.CurrentCamera = camera
camera.CameraType = Enum.CameraType.Scriptable
if block.PrimaryPart then
camera.CFrame = CFrame.new(block.PrimaryPart.Position + Vector3.new(5, 5, 5), block.PrimaryPart.Position)
end
local costLabel = Instance.new("TextLabel")
costLabel.Size = UDim2.new(1, 0, 0, 20)
costLabel.Position = UDim2.new(0, 0, 1, -20)
costLabel.Text = "Cost: " .. cost.Value
costLabel.Parent = blockButton
local displayBlock = block:Clone() -- Cloning the block
displayBlock.Parent = viewport -- Setting the clone as the child of viewport
blockButton.MouseButton1Click:Connect(function()
selectedBlock = block
placeBlock()
blocksShowcase.Visible = false -- Close the GUI after placing the block
end)
end
end
end
end
end
-- Connect the OpenGui button to toggle GUI
if openGuiButton then
openGuiButton.MouseButton1Click:Connect(toggleGui)
end
-- Connect the Close button to close GUI
if closeButton then
closeButton.MouseButton1Click:Connect(closeGui)
end
-- Optionally, you can use UserInputService for touch support
if UserInputService then
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Touch and input.UserInputState == Enum.UserInputState.Begin then
local touchPos = input.Position
local guiPos = openGuiButton.AbsolutePosition
local guiSize = openGuiButton.AbsoluteSize
if touchPos.x >= guiPos.x and touchPos.x <= guiPos.x + guiSize.x and touchPos.y >= guiPos.y and touchPos.y <= guiPos.y + guiSize.y then
toggleGui()
end
end
end)
else
print("Failed to find UserInputService.")
end
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local restoreEvent = ReplicatedStorage:WaitForChild("RestoreEvent")
local restoreButton = script.Parent:FindFirstChild("RestoreButton") -- Replace with the actual path to your Restore button
if restoreButton then
restoreButton.MouseButton1Click:Connect(function()
restoreEvent:FireServer()
end)
end
PlacementSystem in ServerScriptService
-- Server-Side Script: PlacementSystem
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("PlayerBlocksDataStore")
local restoreEvent = Instance.new("RemoteEvent")
restoreEvent.Name = "RestoreEvent"
restoreEvent.Parent = ReplicatedStorage
local placementEvent = Instance.new("RemoteEvent")
placementEvent.Name = "PlacementEvent"
placementEvent.Parent = ReplicatedStorage
-- Table to store placed blocks for each player
local playerBlocks = {}
local function onBlockPlaced(player, blockName, plotIdentifier)
print("Received request to place block: " .. blockName)
local builder = ReplicatedStorage:FindFirstChild("Builder")
if builder then
local blockFound = builder:FindFirstChild(blockName)
if blockFound then
local blockToPlace = blockFound:Clone()
local plot = game.Workspace:FindFirstChild(plotIdentifier)
if plot then
blockToPlace.Position = plot.Position + Vector3.new(0, 10, 0)
blockToPlace.Parent = plot
blockToPlace.Anchored = true
print("Block placed successfully")
if not playerBlocks[player.UserId] then
playerBlocks[player.UserId] = {}
end
table.insert(playerBlocks[player.UserId], blockToPlace)
else
print("Invalid plot identifier")
end
else
print("Block not found: " .. blockName)
end
else
print("Builder folder not found in ReplicatedStorage.")
end
end
-- Connect the function to handle the event
placementEvent.OnServerEvent:Connect(onBlockPlaced)
-- Function to save player blocks
local function savePlayerBlocks(player)
local userId = player.UserId
local blocks = playerBlocks[userId]
if blocks then
local blockData = {}
for _, block in pairs(blocks) do
table.insert(blockData, {Name = block.Name, Position = block.Position})
end
local success, errorMessage = pcall(function()
myDataStore:SetAsync(userId, blockData)
end)
if not success then
print("Failed to save data: ", errorMessage)
end
end
end
-- Function to restore player blocks
local function restoreBlocks(player)
local userId = player.UserId
local success, blockData = pcall(function()
return myDataStore:GetAsync(userId)
end)
if success and blockData then
for _, data in pairs(blockData) do
local blockToPlace = ReplicatedStorage:FindFirstChild("Builder"):FindFirstChild(data.Name):Clone()
if blockToPlace then
blockToPlace.Position = data.Position
blockToPlace.Parent = game.Workspace:FindFirstChild(player.Name .. "_Plot")
blockToPlace.Anchored = true
end
end
end
end
-- Listen for the restore event
restoreEvent.OnServerEvent:Connect(restoreBlocks)
-- Listen for when a player leaves to save their blocks
Players.PlayerRemoving:Connect(savePlayerBlocks)
The output i’m getting is:
15:24:35.595 Attempting to place block… - Client - PlacementGuiScript:41
15:24:35.758 Placing block… - Client - PlacementGuiScript:47
15:24:35.839 Received request to place block: Step - Server - PlacementSystem:21
15:24:35.839 Block not found: Step - Server - PlacementSystem:43
any help would be great.