How do i make a rocket that follows my mouse cursor via a localscript appear for every player

i have a button which when clicked spawns a rocket that follows the players mouse but the rocket it only visible to the player and not everyone else how would I make it so that everyone can see it?
i’ve tried using remote events but no success

local active
local Tool = script.Parent
local rs = game:GetService("ReplicatedStorage")
local eventsfolder = rs:WaitForChild("EventsFolder")
local boomevent = eventsfolder:WaitForChild("Boom")
local moveevent = eventsfolder:WaitForChild("Moverocket")
local folder = rs:WaitForChild("Sussylocalscript")
local Rocket = folder:WaitForChild("Rocket"):Clone()
local plr = game.Players.LocalPlayer
local Debris = game:GetService('Debris')
local mouse = plr:GetMouse()
--rocketMesh.TextureId = "http://www.roblox.com/asset/?id=31601599"

local debris = game:GetService("Debris")

local swooshSound = script.Swoosh
local explosionSound = script.Explosion
local oldcurson = mouse.Icon
local vCharacter
local vPlayer
local params = RaycastParams.new(Rocket)
function blow(hit, missile)
	active = false
	mouse.Icon = oldcurson
	game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
game.Workspace.CurrentCamera.CameraType = "Custom"
	if missile  == nil then return end
	if swooshSound then swooshSound:stop() end
	
	boomevent:FireServer(missile.CFrame)
	if explosionSound then explosionSound:Play() end
	
	if missile then missile:Destroy() end
end



function fire(vTarget)
	
	local vCharacter = plr.Character
	local vHandle = vCharacter:findFirstChild("HumanoidRootPart")
	if vHandle == nil then
	end
	mouse.Icon = "rbxassetid://12751601230"
	local direction = vTarget - vHandle.Position
	direction = computeDirection(direction)
	local missile = Rocket:clone()
	local pos = Vector3.new(939.887, 291.665, -812.581) + (direction * 10.0)		
	missile.CFrame = CFrame.new(pos,  pos + direction) * CFrame.Angles(0, math.pi/2, 0)


	local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)

	if vPlayer == nil then
		print("Player not found")
	else
		if (vPlayer.Neutral == false) then -- nice touch
			missile.BrickColor = vPlayer.TeamColor
		end
	end
	
	local floatForce = Instance.new("BodyForce")
	floatForce.force = Vector3.new(0, missile:GetMass() * 196.1, 0.0)
	floatForce.Parent = missile

	missile.Velocity = computeDirection(vCharacter.Humanoid.TargetPoint - missile.Position) * 20.0
	

	if swooshSound then swooshSound:Play() end

	missile.Touched:connect(function(hit) blow(hit, missile) end)

	debris:AddItem(missile, 100.0)
	local cam = workspace.CurrentCamera
cam.CameraSubject = missile
cam.CameraType = "Follow"
active = true
local parent = plr.Character
	
while(active) do
		direction =  computeDirection(mouse.Hit.Position - missile.Position)
	missile.Velocity = direction * 150
 
	--The below line of code wasn't in the tutorial.  It's optional and just points the missile in the right direction
		missile.CFrame = CFrame.new(missile.Position,  missile.Position + direction) * CFrame.Angles(math.pi/2, 0, 0)
		moveevent:FireServer(missile.CFrame)
	task.wait()
	end
	
end

function computeDirection(vec)
	local lenSquared = vec.magnitude * vec.magnitude
	local invSqrt = 1 / math.sqrt(lenSquared)
	return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end

local db = true
function onActivated()
	if not db then
		return
	end
	
	db = false

	local character = plr.Character;
	local humanoid = character.Humanoid
	if humanoid == nil then
		print("Humanoid not found")
		return 
	end
	
	swooshSound = script:FindFirstChild("Swoosh")
	explosionSound = script:FindFirstChild("Explosion")

	local targetPos = humanoid.TargetPoint
	
	fire(targetPos)

	wait(0.1)

	db = true
end


script.Parent.Activated:connect(onActivated)


In here:

you dont have actually to send the CFrame everytime to the server to replicate it to other players, my idea is to clone the rocket from the server itself and set the rocket network owner to the player that you want to control it so when you change the velocity of it on the client, it will be replicated to other clients

To make the rocket visible to everyone, you need to replicate it to the server and then replicate it to all the clients in the game. You can use RemoteEvents to achieve this.

First, you need to create a RemoteEvent in ReplicatedStorage that will be used to send the rocket to the server. Let’s call it “RocketEvent”.

Then, in the “blow” function, you need to fire the RocketEvent and pass the missile’s CFrame as an argument. This will send the rocket’s position and orientation to the server.

Next, create a new script in ServerScriptService to handle the RocketEvent. In this script, listen for the RocketEvent and create a new rocket object using the passed CFrame. Once the rocket object is created, replicate it to all the clients using RemoteEvent or RemoteFunction.

Here’s an example of what the code might look like:

Client:

local RocketEvent = game:GetService("ReplicatedStorage"):WaitForChild("RocketEvent")

function blow(hit, missile)
    active = false
    mouse.Icon = oldcurson
    game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
    game.Workspace.CurrentCamera.CameraType = "Custom"
    if missile  == nil then return end
    if swooshSound then swooshSound:stop() end
    
    boomevent:FireServer(missile.CFrame)
    RocketEvent:FireServer(missile.CFrame) -- fire RocketEvent and pass missile CFrame
    
    if explosionSound then explosionSound:Play() end
    
    if missile then missile:Destroy() end
end

Server:

local RocketEvent = game:GetService("ReplicatedStorage"):WaitForChild("RocketEvent")
local Rocket = game:GetService("ReplicatedStorage"):WaitForChild("Rocket")

RocketEvent.OnServerEvent:Connect(function(player, cframe)
    -- create new rocket object using passed CFrame
    local newRocket = Rocket:Clone()
    newRocket.CFrame = cframe
    
    -- replicate rocket to all clients
    for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
        local remoteEvent = player:FindFirstChild("RocketRemoteEvent")
        if remoteEvent then
            remoteEvent:FireClient(newRocket)
        end
    end
end)

In the client script, you also need to listen for the RocketRemoteEvent that will be fired by the server to replicate the rocket to the client. Once the event is received, create a new rocket object and set its CFrame to match the one received from the server. Finally, parent the rocket to Workspace so that it becomes visible to everyone.

local RocketRemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RocketRemoteEvent")

RocketRemoteEvent.OnClientEvent:Connect(function(rocketCFrame)
    local newRocket = Rocket:Clone()
    newRocket.CFrame = rocketCFrame
    newRocket.Parent = game.Workspace
end)

I didn’t know camera types use strings!

Yes, camera types in Roblox use strings to specify the type of camera you want to use. The CameraType property of a Camera object can be set to one of the following strings:

  • “Fixed”: A fixed camera that does not move with the player’s character.
  • “Attach”: A camera that is attached to a specified object in the game world.
  • “Watch”: A camera that automatically follows the player’s character.
  • “Track”: A camera that smoothly follows the player’s character, with some lag.
  • “Follow”: A camera that follows the player’s character closely.
  • “Custom”: A camera that you can control and customize using a script.

In your example code, you set the CameraType to “Custom” so that you can control the camera using a script.

Thank you fed bot!

1 Like

the blow function is for when the rocket explodes.

also the rocket follows my mouse cursor like the planes in naval warfare but instead of moving to my mouse.Hit on click it it keeps moving to it even when your not clicking

also what do you mean by

 for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
        local remoteEvent = player:FindFirstChild("RocketRemoteEvent")
        if remoteEvent then
            remoteEvent:FireClient(newRocket)
        end
    end

why would the remote event be inside the player?

Here, check out this place :
Rocket.rbxl (48.8 KB)
it has a tool in StraterPack with simple two explained scripts, I think that what are you looking for, you can edit it depending on your needs.

i solved it more than a week ago

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.