A specific part of my code isn’t working, and I don’t know why.
What it should do is to throw a humanoid, and that humanoid stays still after you throw it until you approach it.
Instead what it does is to throw a humanoid and make it stand still eternally.
Whole script:
local physervice = game:GetService("PhysicsService")
for _, v in pairs(script.Parent:GetDescendants()) do
if v:IsA("BasePart") then
physervice:SetPartCollisionGroup(v, "Bloxmin")
end
end
local db = false
game:GetService("ReplicatedStorage").Throw.OnServerEvent:Connect(function(player, bloxmin, lookvector)
if db == false then
print(Vector3.new(lookvector.X * 75,100,lookvector.Z * 75))
local VictimCharacter = bloxmin
local Humanoid = VictimCharacter:FindFirstChildOfClass("Humanoid")
local RootPart = VictimCharacter:FindFirstChild("HumanoidRootPart") or Humanoid.RootPart
Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, true)
RootPart.AssemblyLinearVelocity = Vector3.new(lookvector.X * 75,100,lookvector.Z * 75)
wait(1)
db = true
end
end)
wait(5)
while db == false do
script.Parent.Humanoid:MoveTo(workspace:FindFirstChild(script.Parent.Values.Leader.Value).PrimaryPart.Position)
wait()
end
while db ~= false do
local distance = (workspace:FindFirstChild(script.Parent.Values.Leader.Value).PrimaryPart.Position - script.Parent.PrimaryPart.Position).Magnitude
if distance <= 5 then
db = false
end
wait()
end
Part that is not working:
while db ~= false do
local distance = (workspace:FindFirstChild(script.Parent.Values.Leader.Value).PrimaryPart.Position - script.Parent.PrimaryPart.Position).Magnitude
if distance <= 5 then
db = false
end
wait()
end
You could just say
while db == true do
and not
while db ~= false do
Though I’m not sure that helps
Nope, doesn’t do anything.
I think youre missing an end statement in the code.
I don’t think so.
The code doesn’t have anything underlined, and the previous code to the broken part works perfectly.
Also, I don’t know if this information helps, but I added a print(distance) after the distance variable, and it doesn’t print anything (Not even a blank space).
And when I added a print(“seinfeld”) (Don’t ask) after the variable it prints seinfeld, but only once.
That means the condition db ~= false isnt happening.
Weird. If db ~= false isn’t happening, but also db == false isn’t happening, then what is db’s value? Did it just vanish? Is it a bug or something?
No what could be happening is that db is true, the second to last while loop is skipped and the last one runs only once because db is set to false in that loop. However code doesnt run backwards so the first loop was skipped.
Now that i look back at your code i dont think while db == false will ever run because you always set db to true before that loop happens.
Ok, now it works. But I have another issue. The humanoid I throw always walks back to me, and I want it to stay still until I approach.
The code now looks like this:
local physervice = game:GetService("PhysicsService")
for _, v in pairs(script.Parent:GetDescendants()) do
if v:IsA("BasePart") then
physervice:SetPartCollisionGroup(v, "Bloxmin")
end
end
local db = false
game:GetService("ReplicatedStorage").Throw.OnServerEvent:Connect(function(player, bloxmin, lookvector)
if db == false then
print(Vector3.new(lookvector.X * 75,100,lookvector.Z * 75))
local VictimCharacter = bloxmin
local Humanoid = VictimCharacter:FindFirstChildOfClass("Humanoid")
local RootPart = VictimCharacter:FindFirstChild("HumanoidRootPart") or Humanoid.RootPart
Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, true)
RootPart.AssemblyLinearVelocity = Vector3.new(lookvector.X * 75,100,lookvector.Z * 75)
wait(1)
db = true
end
end)
wait(5)
while true do
while db == false do
script.Parent.Humanoid:MoveTo(workspace:FindFirstChild(script.Parent.Values.Leader.Value).PrimaryPart.Position)
wait()
end
while db == true do
local distance = (workspace:FindFirstChild(script.Parent.Values.Leader.Value).PrimaryPart.Position - script.Parent.PrimaryPart.Position).Magnitude
print(distance)
if distance <= 4 then
db = false
end
wait()
end
wait()
end
Try swapping the places of the last two loops.
1 Like
Also it is very bad practice to create an infinite loop inside of an event connection. Because now each time that event is fired a new infinite loop will start running. So as you play ,more and more infinite while loops are running in the background never stopping.
No, it isn’t inside the event connection, it is outside.
Also, I don’t know if the extra security measure I added is what fixed the problem, or if it was swapping the places, but now it works!
code just in case anyone has the same problem (which I doubt)
local db = false
local throw = false
game:GetService("ReplicatedStorage").Throw.OnServerEvent:Connect(function(player, bloxmin, lookvector)
if db == false then
print(Vector3.new(lookvector.X * 75,100,lookvector.Z * 75))
local VictimCharacter = bloxmin
local Humanoid = VictimCharacter:FindFirstChildOfClass("Humanoid")
local RootPart = VictimCharacter:FindFirstChild("HumanoidRootPart") or Humanoid.RootPart
Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, true)
RootPart.AssemblyLinearVelocity = Vector3.new(lookvector.X * 75,100,lookvector.Z * 75)
db = true
throw = true
wait(3)
throw = false
end
end)
wait(5)
while true do
while db == true do
local distance = (workspace:FindFirstChild(script.Parent.Values.Leader.Value).PrimaryPart.Position - script.Parent.PrimaryPart.Position).Magnitude
print(distance)
if distance <= 4 then
if throw == false then
db = false
end
end
wait()
end
while db == false do
script.Parent.Humanoid:MoveTo(workspace:FindFirstChild(script.Parent.Values.Leader.Value).PrimaryPart.Position)
wait()
end
wait()
end
1 Like