I already have the scripts set up its just that the part doesn’t get sent to server because it doesn’t exist in the server. I need help sending it to the server.
Localscript:
local PlaceObject = game.ReplicatedStorage.PlaceObject
local Building = game.ReplicatedStorage.Building:Clone()
Building.Parent = workspace
local Snap = 1
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.TargetFilter = Building
mouse.Move:Connect(function()
local CframeNew = CFrame.new(math.floor(mouse.Hit.X / Snap + 1)* Snap, math.floor(mouse.Hit.Y / Snap + 1)* Snap, math.floor(mouse.Hit.Z / Snap + 1)* Snap)
Building:SetPrimaryPartCFrame(CframeNew)
end)
mouse.Button1Down:Connect(function()
PlaceObject:FireServer(Building)
end)
Script:
game.ReplicatedStorage.PlaceObject.OnServerEvent:Connect(function(player, Building)
print(Building) -- prints nil
end)
1 Like
Instances can’t be sent through remote or bindable events, period, and it’s the same for their function counterpart. Even if they could be sent, since the building was created on the client, it will only stay on the client and not replicate to the server, which means the server can’t see it, and no other players would see it except the one who spawned it.
The alternative to this is making the client communicate what it wants to do, and letting the server respond according to this communication.
So from the client, you could just record what CframeNew
is supposed to be, using mouse.Move
, and on click, send just the CFrame over to the server for it to place.
So on the client:
-- renamed CframeNew to BuildingCF
local BuildingCF = CFrame.new()
mouse.Move:Connect(function()
BuildingCF = CFrame.new(
math.floor(mouse.Hit.X / Snap + 1) * Snap,
math.floor(mouse.Hit.Y / Snap + 1) * Snap,
math.floor(mouse.Hit.Z / Snap + 1) * Snap
)
end)
mouse.Button1Down:Connect(function()
PlaceObject:FireServer(BuildingCF)
end)
And on the server:
PlaceObject.OnServerEvent:Connect(function(player,BuildingCF)
local building = game.ReplicatedStorage.Building:Clone()
building:SetPrimaryPartCFrame(BuildingCF)
building.Parent = workspace
end)
Correction: Instances can be sent through bindable events and functions, but not remotes. Sorry about that…
3 Likes
How would i get this to stop after it was fired from a TextButton and then placed.
1 Like
The easiest way to do it is either adding a condition for when the remote event should fire, or just disconnecting it when not in use.
a.k.a:
local connection
local function OnActivated()
PlaceObject:FireServer(BuildingCF)
end
--when you want to connect the event
connection = button.Activated:Connect(OnActivated)
--when you want to disconnect the event
if connection then -- doesn't need the conditional if you know it's safe
connection:Disconnect()
end
or:
local shouldBeFiring = true
button.Activated:Connect(function()
if shouldBeFiring then
shouldBeFiring = false -- or something like that
PlaceObject:FireServer(BuildingCF)
end
end)
1 Like
Thank you a lot for helping me with this i just had to change it up a little bit. it turned out as:
local shouldBeFiring = true
button.MouseButton1Click:Connect(function()
shouldBeFiring = true
mouse.Button1Down:Connect(function()
if shouldBeFiring == true then
PlaceObject:FireServer(BuildingCF)
shouldBeFiring = false
end
if shouldBeFiring == false then end
end)
end)
1 Like