How can I know if object value is not nil

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.
image
If there is a humanoid, it’s not empty.
image

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

Please help it doesn’t work

Instead of doing just else, try doing:
elseif script.Parent.Target.Value == nil then.

I’m not sure if you’ve tried this already. Sorry for the bad formatting, I can’t access my desktop at the moment.

3 Likes

It still doesn’t change the walkspeed :coefficients:

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.

2 Likes

Is this in a local script? If so, the Walkspeed will not change on the server.

If he has other values being passed through this object value, then he would need to check what the value is.

It’s not a local script. aaaaa

If this is an ObjectValue, you’re trying to compare a string to an instance

1 Like

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”)

@1Urge is right. I need to check the value. So, it give me what I exactly want

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.

1 Like

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

4 Likes

It doesn’t change the walkspeed even value is connected to the “Humanoid” object.

A better and shorter way to check if an object is or isn’t nil is

if not object then -- If the object is nil

if object then -- if the object is NOT nil
1 Like

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.

You don’t need to worry about it, it only selects humanoids.

If you did make the reference for the humanoid as a :WaitForChild() function, it should work.

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.

(also replying to @crappy_usename )
I already fixed it. but anyway thanks I appreciate you

1 Like

Humanoid physics such as Walkspeed are automatically replicated.

1 Like