Help needed to allow changes to appear to all players

I am currently making a script where clicking on a part increases its size and if you press Q on a part it flings in the direction you are facing.

It works… on your own.
I am still usure about the different script types and the right place to put scripts so I placed this script all into a Local Script in StarterCharacter scripts.

Any advice on how to allow other players to see the objects increase in size would be much appreciated.

local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")

local rayRange = 500

local function MousePosition()
	local Position = UserInputService:GetMouseLocation()
	local screenToWorldRay = workspace.CurrentCamera:ViewportPointToRay(Position.X, Position.Y)
	DVector = screenToWorldRay.Direction * rayRange
	local RaycastParamse = RaycastParams.new()
	RaycastParamse.FilterDescendantsInstances = {Players.LocalPlayer.Character, workspace.Baseplate}
	local raycast = workspace:Raycast(screenToWorldRay.Origin, DVector, RaycastParamse)
	if raycast then
		return raycast.Instance
	else
		return nil
	end
end

local function MouseDetectEnlarge(input)
	local MouseObjectLand = MousePosition()
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if MouseObjectLand ~= nil then
			local Object = MouseObjectLand
			Object.Size += Vector3.new(5, 5, 5)
			Object.Position += Vector3.new(0, 2.5, 0)
		end
	end
end
local function MouseDetectFling(actionName, inputState)
	if actionName == "Fling" and inputState == Enum.UserInputState.Begin  then
		local Object = MousePosition()
		Object:ApplyImpulse(DVector*10000)
	end
end
UserInputService.InputBegan:Connect(MouseDetectEnlarge)
ContextActionService:BindAction("Fling", MouseDetectFling, false, Enum.KeyCode.Q)

In order to make the changes visible to other players, you need to use RemoteEvents to communicate between the client (LocalScript) and the server (Script). Here’s how you can modify your current script to use RemoteEvents:

  1. Create a RemoteEvent in ReplicatedStorage:
  • In the Explorer, go to ReplicatedStorage, right-click on it, and choose “Insert Object”.
  • Select “RemoteEvent” from the list and name it “ResizeAndFlingEvent”.
  1. Modify the LocalScript (inside StarterPlayerScripts) to fire the RemoteEvent:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("ResizeAndFlingEvent")

-- ... (Keep the rest of your script the same)

local function MouseDetectEnlarge(input)
    local MouseObjectLand = MousePosition()
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        if MouseObjectLand ~= nil then
            remoteEvent:FireServer("Enlarge", MouseObjectLand)
        end
    end
end

local function MouseDetectFling(actionName, inputState)
    if actionName == "Fling" and inputState == Enum.UserInputState.Begin  then
        local Object = MousePosition()
        remoteEvent:FireServer("Fling", Object, DVector)
    end
end

-- ... (Keep the rest of your script the same)
  1. Create a new Script (not a LocalScript) inside ServerScriptService to handle the RemoteEvent:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("ResizeAndFlingEvent")

local function onResizeAndFling(player, action, object, direction)
    if action == "Enlarge" then
        object.Size += Vector3.new(5, 5, 5)
        object.Position += Vector3.new(0, 2.5, 0)
    elseif action == "Fling" then
        object:ApplyImpulse(direction * 10000)
    end
end

remoteEvent.OnServerEvent:Connect(onResizeAndFling)

Now, when you click to enlarge a part or press Q to fling it, the LocalScript will fire the RemoteEvent, which in turn will be caught by the Script on the server. The server will then apply the changes, making them visible to all players.

Keep in mind that you might need to add some validation on the server-side script to make sure players can’t abuse the system by enlarging/flinging parts they’re not supposed to.

Also, using FireAllClients can be an option to minimize the server load. However, there are some trade-offs to consider. By using FireAllClients, you’re essentially letting the clients handle the part manipulation themselves, which can lead to possible inconsistencies and synchronization issues between clients. Additionally, it could open the door for exploitation if a malicious user modifies the client-side script.

Thank you a lot.
This helped me understand the different script types and help learn more about Object Oriented Programming.