Part Won't Move For Client, But Will For Server

Good Evening,

I am having trouble with getting a part to move to a random location with a function fired by a Remote Event from a local script. However, the part will move for the server, but not for the client. I am not great with Client-Server related stuff just yet, so I am unsure on how to solve this. Here’s the script, it is a part of a server script under the model:

-- Variables
local user
local event = game.ReplicatedStorage.SendAISOut
local gbEvent = game.ReplicatedStorage.BeginGame
local noob = script.Parent
local touches = 0

-- Functions
game.Players.PlayerAdded:Connect(function(plyr) -- Player Script
	user = plyr
end)

script.Parent.Touched:connect(function(obj) -- Touched Script
	if game.Players:GetPlayerFromCharacter(obj.Parent) then
		
		local hp = user.Backpack:FindFirstChild("HotPockets") -- Finds Items
		local ed = user.Backpack:FindFirstChild("EnergyDrink")
		local qu = user.Backpack:FindFirstChild("Quarter")
		
		if hp then
			hp:Destroy()
			noob.mine_now:Play()
			noob.Position = Vector3.new(-117.75, 4, 63.5) -- Goes Back to 1st Position
			noob.Position = Vector3.new(0, 180, 0)
		elseif ed then
			noob.mine_now:Play()
			ed:Destroy()
			noob.Position = Vector3.new(-117.75, 4, 63.5) -- Goes Back to 1st Position
			noob.Position = Vector3.new(0, 180, 0)
		elseif qu then
			noob.mine_now:Play()
			qu:Destroy()
			noob.Position = Vector3.new(-117.75, 4, 63.5) -- Goes Back to 1st Position
			noob.Position = Vector3.new(0, 180, 0)
		else
			touches += 1
			wait()
			if touches == 1 then
				noob.no_pass:Play()
				wait(8)
				touches = 0
			end
		end

	else
		-- NPC
	end
end)

local function nextPosition()
	
	wait(math.random(1,1)) -- Change to 60,220
	
	local ranNum = math.random(1,1)
	if ranNum == 1 then
		noob.Position = Vector3.new(38, 5, 31.25)
		noob.Orientation = Vector3.new(0, 90, 0)
	elseif ranNum == 2 then
		noob.Position = Vector3.new(-4, 5, 114.25)
		noob.Orientation = Vector3.new(0, 90, 0)
	elseif ranNum == 3 then
		noob.Position = Vector3.new(-10.25, 5, 127)
		noob.Orientation = Vector3.new(0, 180, 0)
	elseif ranNum == 4 then
		noob.Position = Vector3.new(26.25, 5, 97)
		noob.Orientation = Vector3.new(0, 180, 0)
	end
	noob.give_great:Play()
	print("Noob Moved!")
end

gbEvent.OnServerEvent:Connect(nextPosition)

I’m not sure why it is not working for the client. As I have said, I am still trying to understand more aspects of Client-Server scripting, so I am not great at it. What am I doing wrong?

The issue is with the replication of the part’s movement from the server to the client. When you change the position of the part on the server, the client needs to be notified of this change so that it can update the position on its end.

To solve this issue, you need to use the ReplicatedStorage service to replicate the part’s position change from the server to the client. You can do this by adding a ReplicatedStorage object and a RemoteEvent to your game, and then use the RemoteEvent to fire a function on the client that updates the position of the part.

Here’s an example of how you can modify your code to replicate the part’s position change:

  1. Create a RemoteEvent in ReplicatedStorage and name it “UpdatePartPosition”.
  2. In your server script, modify the nextPosition function to fire the UpdatePartPosition RemoteEvent after updating the position of the part:
local function nextPosition()
	
	wait(math.random(1,1)) -- Change to 60,220
	
	local ranNum = math.random(1,1)
	if ranNum == 1 then
		noob.Position = Vector3.new(38, 5, 31.25)
		noob.Orientation = Vector3.new(0, 90, 0)
	elseif ranNum == 2 then
		noob.Position = Vector3.new(-4, 5, 114.25)
		noob.Orientation = Vector3.new(0, 90, 0)
	elseif ranNum == 3 then
		noob.Position = Vector3.new(-10.25, 5, 127)
		noob.Orientation = Vector3.new(0, 180, 0)
	elseif ranNum == 4 then
		noob.Position = Vector3.new(26.25, 5, 97)
		noob.Orientation = Vector3.new(0, 180, 0)
	end
	noob.give_great:Play()
	print("Noob Moved!")
	game.ReplicatedStorage.UpdatePartPosition:FireServer(noob)
end
  1. In your client script, add a function that listens for the UpdatePartPosition RemoteEvent and updates the position of the part on the client:
game.ReplicatedStorage.UpdatePartPosition.OnClientEvent:Connect(function(part)
	part.Position = Vector3.new(38, 5, 31.25)
	part.Orientation = Vector3.new(0, 90, 0)
end)

This way, whenever the nextPosition function is called on the server, it will fire the UpdatePartPosition RemoteEvent , which will then be received by the client and update the position of the part on the client’s end. LMK if this works.

Should I put the Client Script in ServerStorage? (There was never a client script for the model at first.)

No, if you want the client to receive updates about the part’s position and orientation, you need to use ReplicatedStorage or another Replicated folder, instead of ServerStorage , which is only accessible by the server.

In your case, you have already created the gbEvent remote event in ReplicatedStorage , so you can use that to communicate with the client. You can modify the code in the following way:

In the server script:

gbEvent.OnServerEvent:Connect(function()
  local ranNum = math.random(1,4)
	if ranNum == 1 then
		noob.Position = Vector3.new(38, 5, 31.25)
		noob.Orientation = Vector3.new(0, 90, 0)
	elseif ranNum == 2 then
		noob.Position = Vector3.new(-4, 5, 114.25)
		noob.Orientation = Vector3.new(0, 90, 0)
	elseif ranNum == 3 then
		noob.Position = Vector3.new(-10.25, 5, 127)
		noob.Orientation = Vector3.new(0, 180, 0)
	elseif ranNum == 4 then
		noob.Position = Vector3.new(26.25, 5, 97)
		noob.Orientation = Vector3.new(0, 180, 0)
	end
	noob.give_great:Play()
	print("Noob Moved!")
end)

In a new client script:

local gbEvent = game.ReplicatedStorage.BeginGame
gbEvent.OnClientEvent:Connect(function()
  local noob = script.Parent
	local ranNum = math.random(1,4)
	if ranNum == 1 then
		noob.Position = Vector3.new(38, 5, 31.25)
		noob.Orientation = Vector3.new(0, 90, 0)
	elseif ranNum == 2 then
		noob.Position = Vector3.new(-4, 5, 114.25)
		noob.Orientation = Vector3.new(0, 90, 0)
	elseif ranNum == 3 then
		noob.Position = Vector3.new(-10.25, 5, 127)
		noob.Orientation = Vector3.new(0, 180, 0)
	elseif ranNum == 4 then
		noob.Position = Vector3.new(26.25, 5, 97)
		noob.Orientation = Vector3.new(0, 180, 0)
	end
	noob.give_great:Play()
end)