I am creating a placement system. In my system, I have something called “carry capacity.” Now, I want to make my code repeat placing products until the carry capacity limit is reached. It’s kind of working but not fully. Hopefully, someone could help me.
here is video of my problem:
https://streamable.com/g1zhe0
my code (Local script \ StarterPlayerScripts):
local gridSize = script:FindFirstChild("GridSize").Value -- Change this to adjust grid size
game.ReplicatedStorage.Events.TogglePlacement.OnClientEvent:Connect(function(structureName, carryCapacity)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local camera = workspace.CurrentCamera
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local structures = ReplicatedStorage:FindFirstChild("Structures")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = char:FindFirstChild("HumanoidRootPart")
local maxPlacingDistance = script:FindFirstChild("maxPlacingDistance").Value
local rKeyIsPressed = false
local placingStructure = false
local clientStructure = nil
local yOrientation = 0
local okToPlace = false
local placedCount = 0 -- Track how many structures have been placed
local function placeStructure()
placingStructure = true
clientStructure = structures:FindFirstChild(structureName):Clone()
local PlacementGui = player.PlayerGui:FindFirstChild("PlacementContorlls")
local MainFrame = PlacementGui:FindFirstChild("Main")
if clientStructure then
if MainFrame.Visible == false then
MainFrame.Visible = true
end
clientStructure.CanCollide = false
clientStructure.Parent = game.Workspace
local selectionBox = Instance.new("SelectionBox")
selectionBox.Adornee = clientStructure
selectionBox.Parent = clientStructure
selectionBox.LineThickness = -1
selectionBox.SurfaceColor3 = script:FindFirstChild("PlaceableColor").Value
selectionBox.SurfaceTransparency = 0.75
RunService.RenderStepped:Connect(function()
if not placingStructure then return end
local mousePosition = UIS:GetMouseLocation()
local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {clientStructure, char}
local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)
if raycastResult then
local targetPosition = raycastResult.Position
local hitPart = raycastResult.Instance
local canPlaceFolder = workspace:FindFirstChild("CanPlaceProducts")
okToPlace = false
local anotherCanPlaceFolder = game.Workspace.RestockedProducts
if (canPlaceFolder and hitPart:IsDescendantOf(canPlaceFolder)) or
(anotherCanPlaceFolder and hitPart:IsDescendantOf(anotherCanPlaceFolder)) then
okToPlace = (HumanoidRootPart.Position - targetPosition).Magnitude < maxPlacingDistance
end
clientStructure.CanCollide = true
-- Snap to grid
targetPosition = Vector3.new(
math.floor(targetPosition.X / gridSize + 0.5) * gridSize,
targetPosition.Y + (clientStructure.Size.Y / 2),
math.floor(targetPosition.Z / gridSize + 0.5) * gridSize
)
-- Update SelectionBox color based on placement status
selectionBox.SurfaceColor3 = okToPlace and script:FindFirstChild("PlaceableColor").Value or script:FindFirstChild("NonPlaceableColor").Value
clientStructure.CFrame = CFrame.new(targetPosition) * CFrame.Angles(0, math.rad(yOrientation), 0)
end
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
rKeyIsPressed = true
while rKeyIsPressed do
wait(0.1)
if placingStructure then
yOrientation = yOrientation + script:FindFirstChild("RotateDegree").Value
end
end
elseif input.UserInputType == Enum.UserInputType.MouseButton1 and placingStructure and okToPlace then
clientStructure.CanCollide = true
game.ReplicatedStorage.Events.PlaceStructureEvent:FireServer(clientStructure.Name, clientStructure.CFrame)
placedCount = placedCount + 1 -- Increment placed count
if placedCount <= carryCapacity then
clientStructure:Destroy() -- Destroy the client structure
placeStructure() -- Allow placement of another structure
else
if MainFrame.Visible == true then
MainFrame.Visible = false
end
placingStructure = false
clientStructure:Destroy()
print("Carry capacity reached!")
end
elseif input.KeyCode == Enum.KeyCode.Q and placingStructure then
placingStructure = false
if MainFrame.Visible == true then
MainFrame.Visible = false
end
clientStructure:Destroy()
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
rKeyIsPressed = false
end
end)
else
warn("Structure not found: " .. structureName)
end
end
placeStructure() -- Start the placement process
end)
as you can see I have 3 max carry capacity and when I am trying to place it gives me reach to the max(you can see print) but here’s the problem. when I come second time to take products and place then it giving me the option to get into negative amount, I want to make it able to calculate how much left inside the storage and how much can I can carry up and then it will allow me that amount.
second problem is when I first time trying to place I have 5 left in stock and I can place 3 and when I place the first one its changing the amount to 4 and after I place second one instead giving me 3 its giving me 2 why??
video:
https://streamable.com/n2vwpr
server side code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructureEvent = ReplicatedStorage.Events:WaitForChild("PlaceStructureEvent")
local structuresFolder = ReplicatedStorage:WaitForChild("Structures") -- Folder with available structures
local workspaceFolder = game.Workspace:WaitForChild("RestockedProducts") -- Folder in Workspace for placed structures
local ServerStorage = game:GetService("ServerStorage")
local CurrentStock = ServerStorage:FindFirstChild("ProductsCurrentStock")
-- Function to place a structure in the world
local function placeStructure(structureName, cframe, player)
-- Check if the structure exists in the "Structures" folder
local structureTemplate = structuresFolder:FindFirstChild(structureName)
if structureTemplate then
-- Clone the structure, set its CFrame, and parent it to the Workspace
local newStructure = structureTemplate:Clone()
newStructure.CFrame = cframe
newStructure.Parent = workspaceFolder
if CurrentStock and CurrentStock:FindFirstChild(structureName) then
CurrentStock:FindFirstChild(structureName).Value -= 1
end
else
warn("Structure not found:", structureName)
end
end
-- Remote event connection
PlaceStructureEvent.OnServerEvent:Connect(function(player, structureName, cframe)
placeStructure(structureName, cframe, player)
end)
thanks for helping me my last hope you guys.