我正在制作一个迷你项目,其中我制作了一个领导者统计信息,然后在领导者统计信息中是点数,当您离开和加入时,数据集和检索分别,跟随玩家的大小,并在触及时更改点数由玩家,除了我有时会出错,有时不会
脚本是:
local DatastoreService=game:GetService("DataStoreService")
local zombiePointIncrease=DatastoreService:GetDataStore("zombiePointIncrease")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats=Instance.new("Folder", player)
leaderstats.Name="leaderstats"
local points=Instance.new("IntValue", leaderstats)
points.Name="points"
local data
local success, errormessage=pcall(function()
data=zombiePointIncrease:GetAsync(player.UserId.. "-cash")
end)
if success then
points.Value=data
else
print("there was a error, getting your data")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage=pcall(function()
zombiePointIncrease:SetAsync(player.UserId.. "-cash", player.leaderstats.points.Value)
end)
if success then
print("Data Successfully saved")
else
print("There was a error while saving your data")
warn(errormessage)
end
end)
一切顺利,然后我得到一个僵尸模型并在它下面制作这个脚本, 它基本上会寻找其他玩家并在 agroDistance 内跟随他们:
local zombieHumanoid=script.Parent:WaitForChild("Humanoid")
local zombieTorso=script.Parent:WaitForChild("HumanoidRootPart")
local function findTarget()
local agroDistance=100
local target
for i, v in pairs(game.Workspace:GetChildren()) do
local human=v:FindFirstChild("Humanoid")
local torso=v:FindFirstChild("HumanoidRootPart")
if human and torso and v~=script.Parent then
--check distance
if (torso.Position-zombieTorso.Position).magnitude<agroDistance then
agroDistance=(torso.Position-zombieTorso.Position).magnitude
target=torso
return target
end
end
end
end
while wait(1) do
local torso=findTarget()
if torso then
zombieHumanoid:MoveTo(torso.Position)
else
zombieHumanoid:MoveTo(zombieTorso.Position + Vector3.new(-50, 50),0,Vector3.new(-50,50))
end
end
然后我制作这个脚本,当躯干被触摸时,点数会增加:
local zombietorso=script.Parent.HumanoidRootPart
local Players=game:GetService("Players")
local function findHuman()
for i,v in pairs(game.Workspace:GetChildren()) do
local human=v:FindFirstChild("Humanoid")
local torso=v:FindFirstChild("HumanoidRootPart")
if human and torso and v~=script.Parent then
local player=game.Players:GetPlayerFromCharacter(v)
local points=player.leaderstats.points
points.Value=points.Value+1
end
end
end
zombietorso.Touched:Connect(findHuman())
我的代码似乎没有任何问题,没有语法错误,什么都没有,至少据我所知
但是当使用第三个脚本时,我收到错误指向
zombieHumanoid:MoveTo(zombieTorso.Position + Vector3.new(-50, 50),0,Vector3.new(-50,50))
错误显示“启用将值转换为对象”,当我删除第三个脚本时,有时即使第三个脚本存在,它也可以正常工作,这是 roblox studio 中的错误吗?不会对 roblox 感到惊讶
我已经尝试了一切我能做的,改变脚本变量什么的,这与脚本无关,甚至使用打印语句来调试,它只是有时有效
我发现您的代码存在一些问题,Vector3.new 构造函数方法仅接受 3 个参数,而不是两个,即 x、y 和 z。 至于 MoveTo 方法,它最多只接受两个参数。
修改你的代码,它看起来像这样:
zombieHumanoid:MoveTo(Vector3.new(zombieTorso.Position + Vector3.new(-50, 50, 0),0,Vector3.new(-50,50, 0)))
我在 vector3 构造函数中添加了值 0 作为 Z 向量,因为我不太确定你想在这里实现什么。
相关文档: 移至文档 Vector3 文档