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

Search for a player with a value?

Asked by 8 years ago

Okay, I am making a cash register and I am making it so when a player touches the invisible touch brick in front of the register it sets the value of the player.

The value sets the players name and I want to search the players for that player name.

How would I do that?

This is my script that doesn't work:

Button = script.Parent
playerName = script.Parent.Parent.Parent.Parent.NameValue.Value
local p = game.Players:FindFirstChild(playerName)
Price = script.Parent.Parent.Parent.Parent.Register.MoneyCharged
Button.MouseButton1Click:connect(function() 
    p.Money.Value = p.Money.Value - Price.Value
    Price.Value = 0
end)

1 answer

Log in to vote
2
Answered by 8 years ago

Firstly, for the playerName variable, get rid of the .Value at the end of the line because variables keep a local copy of the value of a property when you directly set it as a variable.

Secondly, I would check for the player when you click the button as the player could be nil if the player leaves. Or, if the playerName changes, it won't update correctly and it'll get the wrong player.

Also, make sure your hierarchy is correct in your script, otherwise your script will not work.

Overall, you should get something like this:

local Button = script.Parent --Local variables are obtained faster than global variables.
local playerName = script.Parent.Parent.Parent.Parent.NameValue --Variables keep a copy of the property's value when you directly set a property as a variable.
local Price = script.Parent.Parent.Parent.Parent.Register.MoneyCharged
Button.MouseButton1Click:connect(function() 
    local p = game.Players:FindFirstChild(playerName.Value) --Directly point to the value property when finding the player to make sure it's looking for the right player.
    if not p then return end --If the player isn't found, stop the function.
    p.Money.Value = p.Money.Value - Price.Value
    Price.Value = 0
end)

I hope my answer helped you. If it did, be sure to accept it.

Ad

Answer this question