英文:
How to make the part move with every player?
问题
以下是翻译好的代码部分:
第一个有效的代码:
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)
我尝试让其工作的代码:
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
我还尝试了这个代码:
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)
以及这个:
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
英文:
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
答案1
得分: 0
你可以使用本地脚本进行操作,但是范围将对其他玩家不可见。
local player = game.Players.LocalPlayer
local range = workspace.Range -- 范围的路径
player.CharacterAdded:Connect(function(character)
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
while humanoidRootPart do
range.Position = humanoidRootPart.Position
wait(0.05)
end
end)
你需要将这个脚本放在 StarterPlayer > StarterPlayerScripts
中,这样脚本将在每个玩家中运行。
但范围的移动不够平滑,要使它更平滑地移动,你可以不是每隔0.05秒移动一次,而是使用 RunService 在每帧移动:
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local range = workspace.Range -- 范围的路径
player.CharacterAdded:Connect(function(character)
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
runService.RenderStepped:Connect(function()
range.Position = humanoidRootPart.Position
end)
end)
为了使范围对其他玩家可见,你需要使用常规脚本。
local runService = game:GetService("RunService")
local range = script.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 -- 存储所有范围的文件夹
while humanoidRootPart do
localRange.Position = humanoidRootPart.Position
wait(0.05)
end
end)
end)
但在这种情况下,你不能使用 RenderStepped。还要记住,如果你的网络连接不太好,范围可能会出现延迟。
希望这有所帮助!
英文:
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论