I have a npc that targets people and kill them. Now, I have a object value called target which defines the humanoid to the script. If there is not any humanoid, value is empty.
If there is a humanoid, it’s not empty.
I want to change the walkspeed if npc targeted someone.
My dumb code that doesn't work
if script.Parent.Target.Value == "Humanoid" then -- if we have a target
script.Parent.Parent.Humanoid.WalkSpeed = 20 --make the npc run
else --if we don't have a target
script.Parent.Parent.Humanoid.WalkSpeed = 12 --keep the walk speed deafult (mine deafult is 12 not 16)
end
if script.Parent.Target.Value then -- if we have a target
script.Parent.Parent.Humanoid.WalkSpeed = 20 --make the npc run
else --if we don't have a target
script.Parent.Parent.Humanoid.WalkSpeed = 12 --keep the walk speed deafult (mine deafult is 12 not 16)
end
Currently, you are checking if an Object.Value (which is an Instance) is equal to “Humanoid” (which is a string). You need to simply check if the Object.Value exists.
I would say if script.Parent.Target.Value ~= nil
I didnt code lua for a long time, say me if this is wrong!
And as @Aegians mentioned, Humanoid is not “Humanoid”, but rather an instance. So
script.Parent.Target.Value:IsA(“Humanoid”)
You cannot compare an Instance to a String. Also, the given code is not dependant on what the value is, it is just simply checking if there is a value.
Object values are not string values – they reference the actual object in the game, not providing a string that represents the name of the object you’re referencing. You would have to reference the humanoid, or at least check if there even is a value there:
if script.Parent.Target.Value and script.Parent.Target.Value:IsA("Humanoid") then
script.Parent.Parent.Humanoid.WalkSpeed = 20
else
script.Parent.Parent.Humanoid.WalkSpeed = 12
end
@1Urge While it is true that the WalkSpeed property’s value itself doesn’t replicate to the server if done on the client, the effect will still replicate as the client calculates and updates their own physics because the client has network ownership of their own character
But he wanted to check what object it was. If the object ended up being a BasePart, then he wouldn’t want to send the AI after the BasePart, only Humanoid objects.
Look, I’m guessing you only have an if statement without a loop. I know this is a pretty late response and you probably have it fixed by now, but this is a way for it to work.
You’d need to have a function to check for a change in the value of the ObjectValue you have.
Doing:
Target.Changed:Connect(function(newValue)
print(newValue) -- PRINT IS JUST FOR A CHECK, YOUR CODE GOES HERE
end)
It would print out that new value, in your case, the new Humanoid, or not, if it’s nil.
I hope this helped you even if you probably got it fixed already, good luck.