How do i make this local script to work in server?

It is a LocalScript in StarterPlayer but i want it to be ServerScript, because it doesn’t work in Server also this script is from YouTube tutorial(Pets system script).

I changed RenderStepped() function to task.spawn(function() it worked but pets were freezing FOR CLIENT ONLY.

the main part isRenderStepped function at the end of the script.

local runService = game:GetService("RunService")

local playerPets = workspace:WaitForChild("MainFolder_Workspace"):WaitForChild("PlayerPets")

local circle = math.pi * 2

local player = game.Players.LocalPlayer

local function getPosition(angle, radius)
	local x = math.cos(angle) * radius
	local z = math.sin(angle) * radius
	return x, z
end

game.Workspace.Script.Enabled = false

local function positionPets(character, folder, dt)
	for i, pet in pairs(folder:GetChildren()) do
		if pet:IsA("Model") then
		local radius = 4+#folder:GetChildren()
		local angle = i * (circle / #folder:GetChildren())
		local x, z = getPosition(angle, radius)
		local _, characterSize = character:GetBoundingBox()
		local _, petSize = pet:GetBoundingBox()

		local offsetY = - characterSize.Y/1.5 + petSize.Y/1.5
		local sin = (math.sin(15 * time() + 1.6)/.5)+1
		local cos = math.cos(7 * time() + 1)/4
		
		if pet:FindFirstChild("Attack").Value ~= nil then
			if pet:FindFirstChild("Walks") then
					local position = (pet:FindFirstChild("Attack").Value.PrimaryPart.CFrame * CFrame.new(x, 0, z)).p
					local lookAt = pet:FindFirstChild("Attack").Value.PrimaryPart.Position
				pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(CFrame.new(position, lookAt) * CFrame.new(0, 1 +sin, 0) * CFrame.fromEulerAnglesXYZ(0,0,cos),0.1))
			else
				local position = (pet:FindFirstChild("Attack").Value.PrimaryPart.CFrame * CFrame.new(x, 0, z)).p
				local lookAt = pet:FindFirstChild("Attack").Value.PrimaryPart.Position
				pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(CFrame.new(position, lookAt) * CFrame.new(0, 3+math.sin(time()*7)/2, 0) * CFrame.fromEulerAnglesXYZ(cos,0,0) ,0.1))
				
			end 
		else
			if character.Humanoid.MoveDirection.Magnitude > 0 then
				if pet:FindFirstChild("Walks") then
					pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(character.PrimaryPart.CFrame * CFrame.new(x, offsetY+sin, z) * CFrame.fromEulerAnglesXYZ(0,0,cos),0.1))
				else
					pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(character.PrimaryPart.CFrame * CFrame.new(x, offsetY/2+math.sin(time()*3)+1, z),0.1))
				end 
			else
				if pet:FindFirstChild("Walks") then 
					pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(character.PrimaryPart.CFrame * CFrame.new(x, offsetY, z) ,0.1))
				else
					pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(character.PrimaryPart.CFrame * CFrame.new(x, offsetY/2+math.sin(time()*3)+1, z) ,0.1))
				end
			end
		end
        end
	end
end

runService.RenderStepped:Connect(function(dt) ---I want to replace this to something that will work in server
	for _, PlrFolder in pairs(playerPets:GetChildren()) do
		local Player = game.Players:FindFirstChild(PlrFolder.Name) or nil
		if Player ~= nil then
			local character = Player.Character or nil
			if character ~= nil then
				positionPets(character, PlrFolder, dt)
			end
		end
 	end
end)
2 Likes

You can either use a remote event to do something on the server and send information over to the server with remote event parameters or place the script into StarterCharacterScripts, that way it replicates to the server.

3 Likes

But RenderStepped:Connect(function() is a loop so i think it can’t fire event too fast like this and i changed RenderStepped to Task.spawn (script is in server script) correct me if i am wrong

That is true, remote events cannot be fired that frequently in a short amount of time. That will cause an error in the output. I’m not sure what you are exactly asking for.

1 Like

You could use RunService.Heartbeat or RunService.Stepped on the server (I’d prefer Heartbeat). RenderStepped can only run on the client. Also, may I ask why you want to use the given code in a server script rather than a LocalScript?

1 Like

I want to use it in server because it is a pet follow script and pet is not moving in server.But i also tried Run service.stepped and heartbeat but it freezez in client(everything is alright in server)Maybe i will change networkownership of primary part of the pet to Player?