Hello Developers! I have currently been trying to script a part that follows the mouse.
local uis = game:GetService("UserInputService")
local storage = game:GetService("ReplicatedStorage")
local run = game:GetService("RunService")
local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
local isMoving = false
local movePart
local usingmove = game.StarterPlayer.StarterCharacterScripts.UsIngMove
local stunned = game.StarterPlayer.StarterCharacterScripts.Stunned
local cooldown =game.StarterPlayer.StarterCharacterScripts.Cooldown
local part = workspace:WaitForChild("Blueboy")
local remote = storage.blue
uis.InputBegan:Connect(function(input, isTyping)
if isTyping then return end
if input.KeyCode == Enum.KeyCode.E then
if usingmove.Value == true or stunned.Value == true or cooldown.Value == true then return end
if not isMoving then isMoving = true
movePart = run.RenderStepped:Connect(function()
mouse.TargetFilter = part
part.Position = mouse.Hit.Position
end)
else
movePart:Disconnect()
mouse.TargetFilter = nil
isMoving = false
end
end
end)
The only problem here is that I want to make other players be able to see this. When I tried to do it on server I realized that RenderStepped can only be used in client leaving me with no choice since I don’t know how to make it work without RenderStepped. Anyone knows how to?
You can do this by firing a remote event to the server from the client to update the parts location.
I wrote some demo code that gives every player their own part that follows their mouse
--[[ Server Script ]]--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remote = ReplicatedStorage.RemoteEvent
-- Creates a new part for each player and updates its position when ever it gets
-- a new position from the client
Players.PlayerAdded:Connect(function(player)
local part = Instance.new("Part")
part.Parent = game.Workspace
part.Anchored = true
part.CanCollide = false
part.CanQuery = false
remote.OnServerEvent:Connect(function(reqPlayer, hit)
if player == reqPlayer then
part.Position = hit
end
end)
end)
--[[ Client Script ]]--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local mouse = localPlayer:GetMouse()
local remote = ReplicatedStorage.RemoteEvent
-- On mouse move update position in the server
mouse.Move:Connect(function()
remote:FireServer(mouse.Hit.Position)
end)
Thanks for taking the time of responding, I appreciate it a lot.
I tried the script and it didn’t work at all. I’ve tried it and the parts do not appear.
Where are you putting the scripts? Also you have to make sure there is a remote event in ReplicatedStorage for this to work (as that is how data gets sent out to other clients)