I’m trying to make a building system. I want to fire the Event only Once. When a player Left Clicked, It will fire a Event that builds a wall. But the Event is getting fired multiple times. I added Disconnect() to the script but it’s not Disconnecting. I added Task.Wait() but I want the player to place builds without waiting.
StarterPlayer > StarterCharacterScripts > LocalScript
--// Service
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
--// Variables
local preview = RS:WaitForChild("wood_wall"):Clone()
local event = RS:WaitForChild("placeBuild")
local mouse = game.Players.LocalPlayer:GetMouse()
--functions
local posX
local posY
local posZ
local gridSize = 20
local function Snap()
posX = math.floor(mouse.Hit.X / gridSize + 1) * gridSize
posY = 3 -- or what ever you want
posZ = math.floor(mouse.Hit.Z / gridSize + 1) * gridSize
end
--Main
RunService.RenderStepped:Connect(function()
Snap()
preview.Parent = game.Workspace
preview.Position = Vector3.new(posX, posY, posZ)
local connection ; connection = UserInputService.InputBegan:Connect(function(input, processed)
if processed then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
event:FireServer(preview.Name ,posX, posY, posZ)
--connection:Disconnect()
end
end)
end)
ServerScriptService > Script
local event = game.ReplicatedStorage:WaitForChild("placeBuild")
local SS = game:GetService("ServerStorage")
event.OnServerEvent:Connect(function(plr, build , posX, posY, posZ)
local folder = SS:WaitForChild("builds")
local found = folder:FindFirstChild(build):Clone()
found.Position = Vector3.new(posX, posY, posZ)
found.Parent = game.Workspace
end)