How Would I Sync All Clients For My TD Game

Hello Developers, I Have Created A Tower System For My TD Game But I Got A Problem Soo When I Place A Tower I Am The Only One That See’s That Tower I Know What You Are Thinking “Make The Towers Server Sided” I Can’t Bc My Mobs Are Client Sided Here Is My Code Logic Client Sided Module :

--Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

--Events handler
local Events_Folder = ReplicatedStorage:WaitForChild("RemotesFolder")

--Tower Events
local Tower_Events = Events_Folder:WaitForChild("Tower_Events")
local OnClient = Tower_Events:WaitForChild("OnClient")
local OnServer = Tower_Events:WaitForChild("OnServer")

local Camera = game.Workspace.CurrentCamera

local Tower = {}

function Tower.Spawn(SelectedTower, BlackList)
	local RP = RaycastParams.new()
	RP.FilterType = Enum.RaycastFilterType.Exclude
	RP.FilterDescendantsInstances = BlackList

	local MouseLoc = UIS:GetMouseLocation()
	local Direction = Camera:ViewportPointToRay(MouseLoc.X, MouseLoc.Y)

	local RayResult = workspace:Raycast(Direction.Origin, Direction.Direction * 100, RP)

	if RayResult then
		local part = Instance.new("Part")
		part.Anchored = true
		part.Position = RayResult.Position
		part.Parent = workspace
		part.Name = SelectedTower

		print("Part created")
	else
		print("Mouse is nil")
	end
end


OnClient.OnClientEvent:Connect(Tower.Spawn)

function Tower.SpawnOnClient(SelectedTower, BlackList)
	OnServer:FireServer(SelectedTower, BlackList)
end

return Tower

client side local script:

--client tower module local script handler :

-- Services
local UIS = game:GetService("UserInputService")

-- require
local Tower_Module = require(script.Parent.Parent.Tower_Module:WaitForChild("Tower_Module"))

-- Variables
local SelectedTower = nil

script.Parent.Parent.Parent.Gui.Gui.SELECTGUI.Test.Activated:Connect(function()
	SelectedTower = "String"
end)

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and SelectedTower then
		Tower_Module.SpawnOnClient(SelectedTower, {})
	end
end)

server side module :

--Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--Events handler
local Events_Folder = ReplicatedStorage:WaitForChild("RemotesFolder")

--Tower Events
local Tower_Events = Events_Folder:WaitForChild("Tower_Events")
local OnClient = Tower_Events:WaitForChild("OnClient")
local OnServer = Tower_Events:WaitForChild("OnServer")

local Tower = {}

function Tower.Spawn(player, SelectedTower, BlackList)
	OnClient:FireAllClients(SelectedTower, BlackList)
end

OnServer.OnServerEvent:Connect(Tower.Spawn)

return Tower

server side script:

--require
local Tower_Module = require(script.Parent.Parent.Tower_Module:WaitForChild("Tower_Module"))

any helps is appreciated!

Well, making both the towers and the mobs server-sided is the best approach here, but if not you could look into using RemoteEvent:FireAllClients() to tell all the clients that a tower has been placed.

If you look at the scripts that’s what I am literally doing

Then how exactly is the tower not showing for the other players if that’s the logic you’re using?

Idk I spawn the towers on client and I send a remote to the server and fireallclients and on server event

Ok Soo I Synced It But The Positions Are Not The Same For All Players.

Ok I Fixed It Just Some Debugging

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.