How do i make this script server side?

  1. What do you want to achieve? I want my script to work on server side so other player can be involved and see it work,
    (right now script only work on myself and only i can see the particles, i want everyone to see what script does)

  2. What solutions have you tried so far? ChatGPT and my friends, none solved it but ChatGPT did better lol

The script

-- LocalScript inside StarterPlayerScripts

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local mouse = Players.LocalPlayer:GetMouse()

-- Variables for the folders containing the particle emitters and sound
local immediateParticleFolder = ReplicatedStorage:WaitForChild("ImmediateParticleEmitter") -- Folder for immediate particles
local delayedParticleFolder = ReplicatedStorage:WaitForChild("DelayedParticleEmitter") -- Folder for delayed particles
local sound = ReplicatedStorage:WaitForChild("ClickSound") -- Configure the path to your sound

-- Function to emit all particles from a folder
local function emitAllParticles(part, folder)
	for _, particleEmitter in ipairs(folder:GetChildren()) do
		if particleEmitter:IsA("ParticleEmitter") then
			local emitter = particleEmitter:Clone()
			emitter.Parent = part

			-- Determine emit count from the custom property EmitCount or use a default value
			local count = emitter:GetAttribute("EmitCount") or 0 -- Default emit count

			emitter:Emit(count)  -- Emit the specified number of particles

			-- Optional: Remove the emitter after it's done emitting
			game.Debris:AddItem(emitter, 5)  -- Lasts for 5 seconds
		end
	end
end

-- Function to add random angular velocity
local function addRandomAngularVelocity(part)
	if part:IsA("BasePart") and not part.Anchored then
		-- Create and configure BodyVelocity object
		local bodyVelocity = Instance.new("BodyVelocity")
		bodyVelocity.Velocity = Vector3.new(
			math.random(-500, 1000), 
			math.random(100, 500), 
			math.random(-500, 1000)
		) -- High random velocity in all directions
		bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge) -- Unlimited force in all directions
		bodyVelocity.P = 10000 -- Power of the velocity
		bodyVelocity.Parent = part

		-- Create and configure BodyAngularVelocity object
		local bodyAngularVelocity = Instance.new("BodyAngularVelocity")
		bodyAngularVelocity.MaxTorque = Vector3.new(100000, 100000, 100000) -- Max rotational force
		bodyAngularVelocity.AngularVelocity = Vector3.new(
			math.random(-1000, 1000), 
			math.random(-1000, 1000), 
			math.random(-1000, 1000)
		)
		bodyAngularVelocity.P = 10000 -- Power of the angular velocity
		bodyAngularVelocity.Parent = part

		-- Optionally, set a lifespan for the BodyVelocity and BodyAngularVelocity
		game.Debris:AddItem(bodyVelocity, 0.1)  -- Lasts for 0.1 seconds
		game.Debris:AddItem(bodyAngularVelocity, 3)  -- Lasts for 10 seconds
	end
end

-- Function to play a sound from ReplicatedStorage
local function playSound(part)
	if sound and sound:IsA("Sound") then
		local soundClone = sound:Clone()
		soundClone.Parent = part
		soundClone:Play()

		-- Optional: Remove the sound after it finishes playing
		soundClone.Stopped:Connect(function()
			soundClone:Destroy()
		end)
	end
end



-- Detect mouse click
mouse.Button1Down:Connect(function()
	-- Get the target object that the mouse is hovering over
	local target = mouse.Target

	-- Apply particles, effects, and sound based on the type of the target
	if target then
		local character = target:FindFirstAncestorOfClass("Model")
		if character and Players:GetPlayerFromCharacter(character) then
			-- Target is a player's character
			local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
			if humanoidRootPart then
				-- Emit all particles from the immediate folder
				emitAllParticles(humanoidRootPart, immediateParticleFolder)
				addRandomAngularVelocity(humanoidRootPart)

				-- Play sound
				playSound(humanoidRootPart)

				-- Delay the emission of the second folder's particles
				wait(3)

				-- Emit all particles from the delayed folder
				emitAllParticles(humanoidRootPart, delayedParticleFolder)
wait(0.25)
				-- Kill the player after emitting delayed particles

			end
		elseif target:IsA("BasePart") and not target.Anchored then
			-- Target is an unanchored BasePart
			addRandomAngularVelocity(target)
		end
	end
end)

you will need to have a local script in somewhere like starterplayerscripts to handle mouse input, and fire the server with the mouse target when they click using a remote event.

local script in starterplayerscripts:

local localplr = game.Players.LocalPlayer
local mouse = localplr:GetMouse()

mouse.Button1Down:Connect(function()
	local target = mouse.Target
	if target then
		game.ReplicatedStorage.RemoteEvent:FireServer(target)
	end
end)

server script in serverscriptservice:


local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Variables for the folders containing the particle emitters and sound
local immediateParticleFolder = ReplicatedStorage:WaitForChild("ImmediateParticleEmitter") -- Folder for immediate particles
local delayedParticleFolder = ReplicatedStorage:WaitForChild("DelayedParticleEmitter") -- Folder for delayed particles
local sound = ReplicatedStorage:WaitForChild("ClickSound") -- Configure the path to your sound

-- Function to emit all particles from a folder
local function emitAllParticles(part, folder)
	for _, particleEmitter in ipairs(folder:GetChildren()) do
		if particleEmitter:IsA("ParticleEmitter") then
			local emitter = particleEmitter:Clone()
			emitter.Parent = part

			-- Determine emit count from the custom property EmitCount or use a default value
			local count = emitter:GetAttribute("EmitCount") or 0 -- Default emit count

			emitter:Emit(count)  -- Emit the specified number of particles

			-- Optional: Remove the emitter after it's done emitting
			game.Debris:AddItem(emitter, 5)  -- Lasts for 5 seconds
		end
	end
end

-- Function to add random angular velocity
local function addRandomAngularVelocity(part)
	if part:IsA("BasePart") and not part.Anchored then
		-- Create and configure BodyVelocity object
		local bodyVelocity = Instance.new("BodyVelocity")
		bodyVelocity.Velocity = Vector3.new(
			math.random(-500, 1000), 
			math.random(100, 500), 
			math.random(-500, 1000)
		) -- High random velocity in all directions
		bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge) -- Unlimited force in all directions
		bodyVelocity.P = 10000 -- Power of the velocity
		bodyVelocity.Parent = part

		-- Create and configure BodyAngularVelocity object
		local bodyAngularVelocity = Instance.new("BodyAngularVelocity")
		bodyAngularVelocity.MaxTorque = Vector3.new(100000, 100000, 100000) -- Max rotational force
		bodyAngularVelocity.AngularVelocity = Vector3.new(
			math.random(-1000, 1000), 
			math.random(-1000, 1000), 
			math.random(-1000, 1000)
		)
		bodyAngularVelocity.P = 10000 -- Power of the angular velocity
		bodyAngularVelocity.Parent = part

		-- Optionally, set a lifespan for the BodyVelocity and BodyAngularVelocity
		game.Debris:AddItem(bodyVelocity, 0.1)  -- Lasts for 0.1 seconds
		game.Debris:AddItem(bodyAngularVelocity, 3)  -- Lasts for 10 seconds
	end
end

-- Function to play a sound from ReplicatedStorage
local function playSound(part)
	if sound and sound:IsA("Sound") then
		local soundClone = sound:Clone()
		soundClone.Parent = part
		soundClone:Play()

		-- Optional: Remove the sound after it finishes playing
		soundClone.Stopped:Connect(function()
			soundClone:Destroy()
		end)
	end
end


game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, target)
	if target then
		local character = target:FindFirstAncestorOfClass("Model")
		if character and Players:GetPlayerFromCharacter(character) then
			-- Target is a player's character
			local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
			if humanoidRootPart then
				-- Emit all particles from the immediate folder
				emitAllParticles(humanoidRootPart, immediateParticleFolder)
				addRandomAngularVelocity(humanoidRootPart)

				-- Play sound
				playSound(humanoidRootPart)

				-- Delay the emission of the second folder's particles
				wait(3)

				-- Emit all particles from the delayed folder
				emitAllParticles(humanoidRootPart, delayedParticleFolder)
				wait(0.25)
				-- Kill the player after emitting delayed particles

			end
		elseif target:IsA("BasePart") and not target.Anchored then
			-- Target is an unanchored BasePart
			addRandomAngularVelocity(target)
		end
	end
end)

1 Like