想简单测试计算一下一个玩家相对某物体的距离
在StarterPlayerScript下面创建了一个local Script,然后这样写:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local pos = character.PrimaryPart.Position
print(pos)
发现报空了,请问应该怎么获取啊?
想简单测试计算一下一个玩家相对某物体的距离
在StarterPlayerScript下面创建了一个local Script,然后这样写:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local pos = character.PrimaryPart.Position
print(pos)
发现报空了,请问应该怎么获取啊?
从服务器端创建Script脚本获取到了,是因为玩家角色生成在workspace下,必须由Script而不能是local Script的原因吗?求大佬解答
服务器端这样写就可以:
local players = game:GetService(“Players”)
local player = players.PlayerAdded:Wait()
local character = player.CharacterAdded:Wait()
print(character.PrimaryPart.Position)
另:如果想实时获取玩家相对某物体的位置变动需要连接什么事件?试了下changed好像不行:
local function Dis()
local dis2 = (workspace.Part1.Position-character.PrimaryPart.Position).Magnitude
print(dis2)
end
character.Changed:Connect(Dis)
建议看一遍api文档