Server moving a part through renderstepped, not replicating to the client

Part moving in server not replicating to client why?

local function getUniqueRandomNumber()
	local randomNumber
	repeat
		randomNumber = math.random(1, 1000) -- Generate a random number between 1 and 1000
	until not table.find(numberTable, randomNumber) -- Keep randomizing until the number isn't in the table

	-- Insert the unique number into the table
	table.insert(numberTable, randomNumber)

	return randomNumber
end


-- Function to simulate the projectile's movement and check for collisions
local function simulateProjectile(projectile, direction, speed, range)
	local distanceTraveled = 0
	local overlapParams = OverlapParams.new()
	overlapParams.FilterType = Enum.RaycastFilterType.Include -- Adjust filter type if needed
	overlapParams.FilterDescendantsInstances = {npcFolder} -- Add parts or models you want to detect
	local uniqueNumber = getUniqueRandomNumber()
	local debounces = {}
	local function onStep(deltaTime)
		local moveDistance = speed * deltaTime
		local newPosition = projectile.Position + direction * moveDistance

		-- Update projectile's position
		projectile.CFrame = CFrame.new(newPosition, newPosition + direction)

		-- Check for collisions using GetPartsBoundsInBox
		local partsInBox = workspace:GetPartBoundsInBox(projectile.CFrame, projectile.Size, overlapParams)
		for _, part in ipairs(partsInBox) do
			local hitCharacter = part.Parent
			if hitCharacter and hitCharacter:IsA("Model") and hitCharacter:FindFirstChild("Humanoid") then
				local hitHumanoid = hitCharacter:FindFirstChild("Humanoid")
				if not hitHumanoid.Parent:FindFirstChild("Debounce"..uniqueNumber) then
					-- Deal damage to the character
					local debounce = Instance.new("BoolValue")
					debounce.Name = "Debounce"..uniqueNumber
					debounce.Parent = hitHumanoid.Parent
					debounce.Value = true
					table.insert(debounces,debounce)
					hitHumanoid:TakeDamage(dashDamage)
					print("hit")
					-- Optional: You can add hit effects here

					-- Remove the projectile
					return
				end
			end
		end

		-- Update the distance traveled
		distanceTraveled = distanceTraveled + moveDistance
		if distanceTraveled >= range then
			-- Destroy the projectile if it reaches its maximum range
			for k in pairs(debounces) do
				debounces[k]:Destroy()
			end
			projectile:Destroy()
			local ind = table.find(numberTable,uniqueNumber)
			table.remove(numberTable,ind)
			runService:UnbindFromRenderStep("ProjectileSimulation"..uniqueNumber)
		end
	end

	-- Bind the onStep function to RunService's RenderStepped event
	runService:BindToRenderStep("ProjectileSimulation"..uniqueNumber, Enum.RenderPriority.Last.Value, onStep)
end

-- Usage Example
local startPosition = Vector3.new(0, 5, 0) -- Replace with the actual start position
local direction = Vector3.new(1, 0, 0).Unit -- Replace with the actual direction
local speed = 2.5 -- Speed of the projectile
local range = 200 -- Maximum range the projectile can travel
local size = Vector3.new(4.5, .5, 5) -- Size of the projectile

local projectile = createProjectile(startPosition, direction, speed, range, size)
simulateProjectile(projectile, direction, speed, range)	

RenderStepped is Client-sided meaning it won’t run on a server as servers don’t render stuff (unless you’re viewing on studio)

Physics calculations are usually done on Heartbeat or Stepped but they don’t have a :Bind.. function so you’d have to keep track of the connections manually like so

To bind:

local connection = runService.Heartbeat:Connect(function(deltaTime)
	-- Calculation code here
end)

or

function onStep(deltaTime)
	-- Calculation code here
end

local connection = runService.Heartbeat:Connect(onStep)

To unbind:

connection:Disconnect()

You might need to store these connections in a table to disconnect them later on

1 Like

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