I wanted this model to move only when it receives a event, but it isn’t working. The code is in a serverscript in the model. Also, there are no errors. Thanks, and have a great day/night!
local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")
game.ReplicatedStorage.GetEm.Event:Connect(function()
local function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 500
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while true do
wait(1)
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
script.Parent:MoveTo(target.Position, target)
end
end
end)
13 Likes
This isn’t exactly related to your problem but these tips will help you debug your code better and other people are more likely to help you.
Try not to use deprecated methods and functions such as Instance:children(). Use Instance:GetChildren() instead.
Indent your code properly, this makes your code a heck more easier to read.
Don’t define functions inside of other functions, define them inside of tables or in the global scope for readability.
Try to have meaningful variable names, temp1 and temp2 is very vague. I would also write my constants in lower_snake_case or UPPER_SNAKE_CASE. An exampled would be
local ZOMBIE_DETECTION_RANGE = 100
Use printing to debug your code, print() is a lifesaver.
Also you can do
if variable then
print("variable isn't nil/false")
end
instead of
if variable ~= nil then
print("variable isn't nil")
end
If you find yourself needing to use the second one then you most likely need to rewrite your code.
2 Likes
Ok so I added print statements, but none of them print. Here is the script in which I fire the event:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Range = 50
game:GetService("RunService").RenderStepped:Connect(function()
local ray = Ray.new(Character.Head.Position, Character.Head.CFrame.LookVector * Range)
local hit = workspace:FindPartOnRay(ray)
if hit then
local model = hit.Parent or hit.Parent.Parent
if model and model.Name == "Bob" then
print(model.Name)
else
game.ReplicatedStorage.GetEm:Fire()
print("Fired")
end
end
end)
1 Like
I believe you have missed an end somewhere around line 20. Properly indenting your code would help.
1 Like
No I don’t think so because its not telling the output is not telling me errors
1 Like
Try printing the intersection point and perhaps placing some sort of marker there.
1 Like
Sorry, I don’t get what you mean by that, could you elaborate?
1 Like
As i see you are using the bindable event, and you are using it by local to server, the problem is the bindable function / event (Forgot the event here) runs server to server or local to local, try to use a remote event or fire the bindable from a server.
3 Likes
workspace:FindPartOnRay() returns a tuple if it detects something.
local ray = Ray.new(...)
local part, intersectionPoint, normal, material = workspace:FindPartOnRay(ray)
1 Like
The model was floating and moving, how do I fix that? Also, when I looked at the model, it wouldn’t stop moving
1 Like
Why do I need that? I already have that problem solved, I just need help on my moving script
1 Like
Is it a character (with humanoid) as a custom model?
1 Like
The model only has one part the humanoid root part
1 Like
Try to do like that
while true do
wait(1)
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
local pos = Vector3.new(target.Position.X, script.Parent.HumanoidRootPart.Size.Y / 2, target.Position.Z)
script.Parent:MoveTo(pos, target)
end
end
end)
2 Likes
That would replace this? (30 chrs)
2 Likes
Yeah, because the new position will just change the X and Z, and not the Y (Y will be the size of root part / 2)
1 Like
I don’t think anything changed
1 Like
Keeps flying? Do the model have another part?
1 Like
Yeah its in the humanoidrootpart called ‘hitbox’
Try to set the HumanoidRootPart as primaryPart of the model, and put the hitbox inside of the model.
1 Like