ReplicatedStorage.KeyPressed.OnServerEvent:Connect(function(player, key, db)
if key == Enum.KeyCode.One and db then
print("a")
ReplicatedStorage.FurniturePlacement:FireClient(player, true)
elseif key == Enum.KeyCode.One and not db then
ReplicatedStorage.FurniturePlacement:FireClient(player, false)
print("b")
end
end)
Anyways, UhmTex’s comment has led me to realize that you should place the mouse.Move event outside of OnClientEvent, have OnClientEvent set a variable in the LocalScript, and use that. The way your code is currently set up, every time OCE fires will create a new Move event that continues to print the same thing, as every firing has their own local CreatedFurniture variable.
You mean something along the lines of creating a local variable that has a nil value in the local script, then OCE set that variable to true or false? In that case it would still print out nil?
Unsure if this is important but I should probably mention that code was just a very stripped down code of this.
Local Script
ReplicatedStorage.FurniturePlacement.OnClientEvent:Connect(function(Furniture, CreatedFurniture)
mouse.Move:Connect(function()
print(CreatedFurniture)
if CreatedFurniture then
local SizeY = Furniture.PrimaryPart.Size.Y
local SizeX = Furniture.PrimaryPart.Size.X
local SizeZ = Furniture.PrimaryPart.Size.Z
for i, v in pairs(Furniture:GetChildren()) do
if v.Name ~= "GridPart" then
v.Transparency = 0
end
end
SnapFurniture() -- "Snaps" furniture to grid
mouse.TargetFilter = Furniture
Furniture:SetPrimaryPartCFrame(CFrame.new(Vector3.new(posX + SizeX/2, posY + SizeY/2, posZ + SizeZ/2))) -- Pos values from SnapFurniture function
else
Furniture:Destroy()
end
end)
end)
Server Script
ReplicatedStorage.KeyPressed.OnServerEvent:Connect(function(player, key, db)
if key == Enum.KeyCode.One and db then
local Furniture = ServerStorage.Furniture.WoodTable:Clone()
Furniture.Parent = workspace
for i, v in pairs(Furniture:GetChildren()) do
v.Transparency = 1
end
print("a")
ReplicatedStorage.FurniturePlacement:FireClient(player, Furniture, true)
elseif key == Enum.KeyCode.One and not db then
local Furniture = workspace:FindFirstChild("WoodTable")
ReplicatedStorage.FurniturePlacement:FireClient(player, Furniture, false)
print("b")
end
end)
I omitted it from the local script if that’s what you mean.
In local Script
UserInputService.InputBegan:connect(function(InputObject, gameProcessedEvent)
if InputObject.KeyCode == Enum.KeyCode.One and not KeyPressed then
KeyPressed = true
ReplicatedStorage.KeyPressed:FireServer(Enum.KeyCode.One, true)
elseif InputObject.KeyCode == Enum.KeyCode.One and KeyPressed then
KeyPressed = false
ReplicatedStorage.KeyPressed:FireServer(Enum.KeyCode.One, false)
end
end)
-- Game Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Client Variabke
local player = Players.LocalPlayer
local mouse = player:GetMouse()
-- Other variables
local posX
local posY
local posZ
local gridSize = 1
-- Bools
local KeyPressed = false
UserInputService.InputBegan:connect(function(InputObject, gameProcessedEvent)
if InputObject.KeyCode == Enum.KeyCode.One and not KeyPressed then
KeyPressed = true
ReplicatedStorage.KeyPressed:FireServer(Enum.KeyCode.One, true)
elseif InputObject.KeyCode == Enum.KeyCode.One and KeyPressed then
KeyPressed = false
ReplicatedStorage.KeyPressed:FireServer(Enum.KeyCode.One, false)
end
end)
local function SnapFurniture()
posX = math.floor(mouse.Hit.X / gridSize + 0.5) * gridSize
posY = mouse.Hit.Y
posZ = math.floor(mouse.Hit.Z / gridSize + 0.5) * gridSize
end
ReplicatedStorage.FurniturePlacement.OnClientEvent:Connect(function(Furniture, CreatedFurniture)
mouse.Move:Connect(function()
print(CreatedFurniture)
if CreatedFurniture then
local SizeY = Furniture.PrimaryPart.Size.Y
local SizeX = Furniture.PrimaryPart.Size.X
local SizeZ = Furniture.PrimaryPart.Size.Z
for i, v in pairs(Furniture:GetChildren()) do
if v.Name ~= "GridPart" then
v.Transparency = 0
end
end
SnapFurniture() -- "Snaps" furniture to grid
mouse.TargetFilter = Furniture
Furniture:SetPrimaryPartCFrame(CFrame.new(Vector3.new(posX + SizeX/2, posY + SizeY/2, posZ + SizeZ/2))) -- Pos values from SnapFurniture function
else
Furniture:Destroy()
end
end)
end)
Server Script
-- Game Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
ReplicatedStorage.KeyPressed.OnServerEvent:Connect(function(player, key, db)
if key == Enum.KeyCode.One and db then
local Furniture = ServerStorage.Furniture.WoodTable:Clone()
Furniture.Parent = workspace
for i, v in pairs(Furniture:GetChildren()) do
v.Transparency = 1
end
print("a")
ReplicatedStorage.FurniturePlacement:FireClient(player, Furniture, true)
elseif key == Enum.KeyCode.One and not db then
local Furniture = workspace:FindFirstChild("WoodTable")
ReplicatedStorage.FurniturePlacement:FireClient(player, Furniture, false)
print("b")
end
end)
After on client event fires for the second time, the variable CreatedFurniture should be false. So that if CreatedFurniture then shouldn’t be “firing”.
However it is, and when moving the mouse, it prints “true, false, true, false, true, false” and so on like seen in the gyazo link I posted originally.
When key one is pressed for the first time, it fires a remote event telling the server to clone a model from server storage.
After putting the model parent as workspace, a client event is fired from the server.
On client event fired the model cframe is set to that of the mouses position, and so whenever the mouse moves the models cframe moves with it, this works fine. https://gyazo.com/33e163e130f843d554232bb6793a0275
When firing the second time you are making a new connection to the mouse move event instead of updating the value you want it to use. That is why it is printing out true, false.
You could try something like updating a local variable that a seperate mouse move event uses.
local CreatedFurnitureLocal
local FurnitureLocal
ReplicatedStorage.FurniturePlacement.OnClientEvent:Connect(function(Furniture, CreatedFurniture)
CreatedFurnitureLocal = CreatedFurniture
FurnitureLocal = Furniture
end)
mouse.Move:Connect(function()
print(CreatedFurnitureLocal)
if CreatedFurnitureLocal then
local SizeY = FurnitureLocal.PrimaryPart.Size.Y
local SizeX = FurnitureLocal.PrimaryPart.Size.X
local SizeZ = FurnitureLocal.PrimaryPart.Size.Z
for i, v in pairs(FurnitureLocal:GetChildren()) do
if v.Name ~= "GridPart" then
v.Transparency = 0
end
end
SnapFurniture() -- "Snaps" furniture to grid
mouse.TargetFilter = FurnitureLocal
FurnitureLocal:SetPrimaryPartCFrame(CFrame.new(Vector3.new(posX + SizeX/2, posY + SizeY/2, posZ + SizeZ/2))) -- Pos values from SnapFurniture function
else
FurnitureLocal:Destroy()
end
end)```
I can’t figure the issue, but second-time bugs mean the mouse.Move being assigned multiple times, so try using variables for furniture, also checking for the input KeyCode is unnecessary on the server, you’re already doing it on the client input detection: