I am making a mini project where i make a leaderstats, and then in the leader stats is the points, and when you leave and join, data sets and retrieves respectively, follows the player through magnitude, and changes the points when it gets touched by player, except im getting error sometimes, sometimes not
我正在做一个小项目,我做了一个领袖统计,然后在领袖统计中是点,当你离开和加入时,分别设置和检索数据,跟踪玩家的等级,当玩家接触到它时改变点数,除了我有时会出错,有时不会。
the script is:
脚本是:
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)
All goes fine then i get a zombie model and make this script under it,
it basically looks for other players and follows them within the agroDistance:
一切顺利,然后我得到了一个僵尸模型,并在它下面制作了这个脚本,它基本上是寻找其他玩家,并在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
then i make this script where when the torso gets touched, the points increase:
然后我制作了这个脚本,当躯干被触摸时,点数会增加:
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())
it seems as there is legit no problem in my code, no syntax error, nothing, atleast for what i know
看起来我的代码中没有合法的问题,没有语法错误,什么都没有,至少就我所知是这样
but when the 3rd script is used i get error pointing to
但是当使用第三个脚本时,我得到错误指向
zombieHumanoid:MoveTo(zombieTorso.Position + Vector3.new(-50, 50),0,Vector3.new(-50,50))
the error says "Enable to cast value to Object", when i remove the 3rd script it works fine sometimes even when the 3rd script is there, it works fine, is this a bug within roblox studio? wouldn't be surprised with roblox
当我删除第三个脚本时,它有时工作得很好,即使第三个脚本在那里,它也工作得很好,这是Roblox Studio中的错误吗?对Roblox不会感到惊讶
I have legit tried everything i could, change script variables whatnot which would not have to do with script even using print statements to debug, it only works sometimes
我已经合法地尝试了我能做的一切,改变脚本变量,即使使用打印语句进行调试,也不会与脚本有任何关系,它只在某些时候起作用
更多回答
I see a couple of problems with your code, the Vector3.new constructor method only accepts 3 arguments, not two, these are x, y and z.
As for the MoveTo method, it only accepts at max two arguments.
我发现您的代码有几个问题,Vector3.new构造函数方法只接受3个参数,而不是两个参数,它们是x、y和z。至于moveto方法,它最多只接受两个参数。
Modifying your code, it would look like so:
修改您的代码,它将如下所示:
zombieHumanoid:MoveTo(Vector3.new(zombieTorso.Position + Vector3.new(-50, 50, 0),0,Vector3.new(-50,50, 0)))
I have added the value 0 as the Z vector in the vector3 constructors, as I'm not really sure what you want to achieve here.
我已经在vector3构造函数中添加了值0作为Z向量,因为我不确定你想在这里实现什么。
Related documentation:
MoveTo documentation
Vector3 documentation
相关文档:Moveto文档向量3文档
更多回答
我是一名优秀的程序员,十分优秀!