Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Using ObjectValue as string?

Asked by 8 years ago
OwnerName = script.Parent.Parent.Parent.Owner.Value
while true do
    if script.Parent.Parent.Parent.Injuries.Value > 35 then
        wait (1)
     script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Players[OwnerName].PlayerGui.VisitorPopup.TextButton.Visible = true
        script.Parent.Head.Transparency = 0
        script.Parent.Head.CanCollide = true
        print ("Call Visitor to office appeared!")
        return false
    else
        wait (1)
    end
end

I receive an error trying to use the value of Owner as OwnName when referring to the players. "bad arguement #2 to '?' (string expected, got nil)

Ideally, the script is supposed to take the value of owner, set that as "OwnerName" and use it when referencing Players so the GUI would only appear on that specific player's screen.

I think the error is coming from the fact that Owner.Value is an ObjectValue instead of a StringValue, but for the purposes of what I'm trying to do, I cannot change it to a string. Is there any other way to make this work?

1 answer

Log in to vote
0
Answered by 8 years ago

What's wrong is that you're setting OwnerName to Owner's value. You should instead set it to simply Owner, like below.

Also, I'm not sure if you can implicitly convert an object to a string, but I assume not, so you should use OwnerName.Value.Name

Finally, I noticed that you have a lot of indexing (i.e. script.Parent.Parent.Parent etc.). Since it's running on a loop, this can cause performance issues. Not to mention, it's also very long and unnecessary.

Here is the final script with the improvements and a few extra comments:

local Owner = script.Parent.Parent.Parent.Owner -- Global variables are evil.  Use local variables instead.
while true do
    if script.Parent.Parent.Parent.Injuries.Value > 35 then -- You may wish to optimise this using variables.
        wait (1)
    -- Here I assume the variable is the Player object.  If not, use game.Players[Owner.Value.Name] instead of Owner.Value
     Owner.Value.PlayerGui.VisitorPopup.TextButton.Visible = true
        script.Parent.Head.Transparency = 0
        script.Parent.Head.CanCollide = true
        print ("Call Visitor to office appeared!")
        return false
    else
        wait (1)
    end
end


Ad

Answer this question