Hi! So I wanted to make a placement system, which is almost working. The problem is, by the time you spend in the placing menu, the amount of item it will place. So like if hit the button 3, and wait 2 sec, it will clone 40 items, if I wait 10 sec, it will clone 150 items, and it’s strange.
I used a remoteFunction to make the placing event servered.
Here is the localscript:
local rs = game:GetService("ReplicatedStorage")
local placeExplosives = rs:WaitForChild("PlaceExplosives")
local explosives = rs:WaitForChild("Explosives")
local uis = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local StructureFrame = script.Parent.SlotFrame
local char = player.Character or player.Character:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local yBuildingOffset = 5
local maxPlacingDistance = 10
local rKeyIsPressed = false
local placingStructure = false
uis.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.Three then
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 0
script.Disabled = true
local yOrientation = 0
local goodToPlace = false
local placedStructure
if placingStructure == false then
placingStructure = true
local clientStructure = explosives:FindFirstChild("SmallC4").Base:Clone()
clientStructure.BrickColor = BrickColor.new("White")
clientStructure.CanCollide = false
clientStructure.Parent = game.Workspace
clientStructure.Transparency = .6
clientStructure.Light.Transparency = 1
local startingCFrame = CFrame.new(0, -4.75, 0)
clientStructure.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(startingCFrame)
runService.RenderStepped:Connect(function()
local mouseRay = mouse.UnitRay
local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
local ignoreList = {clientStructure, char}
local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)
if hit and (HumanoidRootPart.Position - clientStructure.Position).magnitude < maxPlacingDistance then
yOrientation = hit.Orientation.Y
goodToPlace = true
clientStructure.BrickColor = BrickColor.new("White")
clientStructure.Orientation = hit.Orientation
else
yOrientation = 0
goodToPlace = false
clientStructure.BrickColor = BrickColor.new("Crimson")
end
local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
local newCFrame = CFrame.new(position.X, position.Y + yBuildingOffset, position.Z)
clientStructure.CFrame = newCFrame * startingCFrame
clientStructure.Rotation = HumanoidRootPart.Orientation + hit.Orientation
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
rKeyIsPressed = true
local rotationSpeed = .1
while rKeyIsPressed do
wait()
if placingStructure == true then
yOrientation = yOrientation + rotationSpeed
end
end
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
rKeyIsPressed = false
end
end)
mouse.Button1Down:Connect(function()
if placingStructure == true then
if goodToPlace == true then
local structureCFrame = clientStructure.CFrame
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
placedStructure = placeExplosives:InvokeServer(clientStructure.Name, structureCFrame, HumanoidRootPart.Orientation+ hit.Orientation, clientStructure.Light.CFrame, clientStructure.Light.Anchored)
placedStructure = true
if placedStructure == true then
script.Disabled = true
placingStructure = false
clientStructure.Transparency = 1
clientStructure:Destroy()
wait(2)
script.Disabled = false
end
end
end
end)
end)
end
end
end)
ServerScript:
local rs = game:GetService("ReplicatedStorage")
local placeExplosives = rs:WaitForChild("PlaceExplosives")
local explosives = rs:WaitForChild("Explosives")
placeExplosives.OnServerInvoke = function(player, ExplosiveName, ExplosiveCFrame, hitCF, lightCF, lightpart)
local crafted
local realExplosive = explosives.SmallC4:FindFirstChild(ExplosiveName).Parent:Clone(print("cloned"))
if realExplosive then
player.Character.Humanoid.WalkSpeed = 16
realExplosive.Base.CFrame = ExplosiveCFrame
realExplosive.Base.Light.CFrame = lightCF
realExplosive.Base.UsePartColor = false
lightpart = true
realExplosive.Parent = game.Workspace
realExplosive.Base.Orientation = hitCF
crafted = true
else
crafted = false
end
return crafted
end
It can spawn the items, but it will clone too many of them for no reason. Thanks for your help!