Intro
I made a few scripts to move parts on the client (using bodypositions) and it works.
However, I’m failing to optimize my scripts and I was hoping one of you can help.
Goal of the script
I wanted to SetNetworkOwner() for the five moving parts in my game. This can only be done in (1) the workspace and (2) using a serverscript. Without SetNetworkOwner I can’t change the bodypositions on the client.
Script number 1: Server Script (ServerScriptService)
Script
local movefolder = game:GetService("ReplicatedStorage").Movefolder
local remote = game.ReplicatedStorage.Movement
game.Players.PlayerAdded:Connect(function(player)
local Clonefolder = movefolder:Clone()
Clonefolder.Name = player.Name
Clonefolder.Parent = game.Workspace.movefolderstore
local blueflower = Clonefolder.BlueFlower
local movingflower = Clonefolder.MovingFlower
local movingplanets = Clonefolder.MovingPlanets
local movingplatform = Clonefolder.MovingPlatform
local Redflower = Clonefolder.RedFlower
blueflower:SetNetworkOwner(player)
Redflower:SetNetworkOwner(player)
movingflower:SetNetworkOwner(player)
movingplatform:SetNetworkOwner(player)
movingplanets:SetNetworkOwner(player)
remote:FireClient(player)
Clonefolder.Parent = game.ReplicatedStorage
end)
Explanation: Player is added. The ‘‘Movefolder’’ in the ReplicatedStorage is cloned (so each player can have ownership). Remote is fired so the Localscript can start working. Clonefolder (named as the player who joined) is sent back to the ReplicatedStorage.
Script number 2: Localscript (Starterplayerscripts)
Localscript
local Player = game.Players.LocalPlayer
local movefolderstore = game.Workspace.movefolderstore
local movefolder = game.ReplicatedStorage:WaitForChild(Player.Name)
local remote = game.ReplicatedStorage.Movement
local blueflower = movefolder.BlueFlower
local redflower = movefolder.RedFlower
local extraflower = movefolder.MovingFlower
local candyplatform = movefolder.MovingPlatform
local planets = movefolder.MovingPlanets
local timer = 4
remote.OnClientEvent:Connect(function()
movefolder.Parent = movefolderstore
while true do
planets.BodyPosition.Position = Vector3.new(-52.935, 8.321, 61)
blueflower.BodyPosition.Position = Vector3.new(-307.369, 12.069, -63.031)
redflower.BodyPosition.Position = Vector3.new(-278.362, 12.069, -50.031)
extraflower.BodyPosition.Position = Vector3.new(-54.371, 2.303, -239.852)
candyplatform.BodyPosition.Position = Vector3.new(170.308, 8.25, -56.95)
wait(timer)
planets.BodyPosition.Position = Vector3.new(-52.935, 8.321, 49)
blueflower.BodyPosition.Position = Vector3.new(-278.362, 11.896, -63.437)
redflower.BodyPosition.Position = Vector3.new(-307.369, 12.069, -50.031)
extraflower.BodyPosition.Position = Vector3.new(-54.371, 1.948, -253.776)
candyplatform.BodyPosition.Position = Vector3.new(151.308, 8.25, -56.95)
wait(timer)
end
end)
Explanation: the server fired the remote. This script now starts doing what is needed: changing the bodypositions in a loop to make the parts move every x seconds. The ‘‘clonedfolder’’ (the folder where the localplayer is networkowner of) is moved to workspace to make it visible for the client only.
Thank you and please ask questions
This post is probably really confusing. I really hope one of you can help me out. Please ask me for anything if needed. I tried to be as clear as possible. Your help is appreciated!