Search code examples
luaroblox

How to make the part move with every player?


I'm trying to make a player range in Roblox Studio. I have a working script, but it only applies to the first player to join the game. I tried to modify it, but it doesn't work. (You can see the first code and reply, you don't have to read everything)

The first code that works:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
        while humanoidRootPart do
            script.Parent.Position = humanoidRootPart.Position
            wait(0.05)
        end
    end)
end)

The code that I tried to make work:

local Players = game:GetService("Players")

local Original_range = script.Parent

local range = Original_range:Clone()

local Player = game.Players.LocalPlayer

range.Parent = workspace

local humanoidRootPart = Player:WaitForChild("HumanoidRootPart")

while humanoidRootPart do
    range.Position = humanoidRootPart.Position
    wait(0.05)
end

I also tried this:

local Players = game:GetService("Players")

local original = script.Parent

local range = original:Clone()

range.Parent = workspace

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
        while humanoidRootPart do
            range.Position = humanoidRootPart.Position
            wait(0.05)
        end
    end)
end)

And this:

local Player = game.PLayers.LocalPlayer

local original = script.Parent

local range = original:Clone()

range.Parent = workspace


local humanoidRootPart = Player:WaitForChild("HumanoidRootPart")

while humanoidRootPart do
    range.Position = humanoidRootPart.Position
    wait(0.05)
end

Solution

  • You can use local script for that, but range will be not visible to other players.

    local player = game.Players.LocalPlayer
    local range = workspace.Range -- path to range
    
    player.CharacterAdded:Connect(function(character)
        local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
    
        while humanoidRootPart do
            range.Position = humanoidRootPart.Position
            wait(0.05)
        end
    end)
    
    

    You need to put this script in StarterPlayer > StarterPlayerScripts, so this script would run in every player.

    But range moves not smoothly, to move it smoothly you can instead of moving it every 0.05 seconds, move it every frame by using RunService:

    local runService = game:GetService("RunService")
    local player = game.Players.LocalPlayer
    local range = workspace.Range -- path to range
    
    player.CharacterAdded:Connect(function(character)
        local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
    
        runService.RenderStepped:Connect(function()
            range.Position = humanoidRootPart.Position
        end)
    end)
    

    To make range visible to other players, you need to use regular script.

    local runService = game:GetService("RunService")
    local range = script.Range -- path to original range
    
    game.Players.PlayerAdded:Connect(function(player)
        player.CharacterAdded:Connect(function(character)
            local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
            local localRange = range:Clone()
            localRange.Parent = workspace.Ranges -- folder where all ranges stored
    
            while humanoidRootPart do
                localRange.Position = humanoidRootPart.Position
                wait(0.05)
            end
        end)
    end)
    

    But in this case, you can't use RenderStepped. Also keep in mind that if you don't have great internet connection range would lag.

    Let me know if it helps!