Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to get a specific player from the online players?

Hey, I'm very new to coding and I want to make a script, where on a press of a button a specific player's integer value will change, here's the script I made:

script.Parent.MouseButton1Up:Connect(function(player)
	local setCashButton = script.Parent
	local playerName = script.Parent.Parent.playerNameTxtBox.Text
	local cashValue = script.Parent.Parent.cashValueTxtBox.Text
	local playerLoc = game.Players.playerName
		
	playerLoc.leaderstats.Cash.Value = cashValue
end)

The problem with this, is that I can't figure out how to search for a specific player that is in the server, the code fails at line 5 when I try to specify the given player's name. Any help would be appreciated!

5 comments

0

You pass in a "player" parameter, I assume that this is a reference to the current player in Players. But if this is a localscript (considering you're taking inputs), leaderstats should only be tampered with by the server in most if not all cases. Use a remoteevent and impose the appropriate checks, I can't tell what'd they be from this script alone.

0

Well this sound very complicated. Any recommendations on how to start this?

0

yes, read api about RemoteEvents

0

also, you can use [variable] to include variables values, you may need to use that on line 5

0

Will do it tomorrow, it's getting late. Thanks for the help!

1 answer

So what you need to do is:

script.Parent.MouseButton1Up:Connect(function(player)
	    local setCashButton = script.Parent
	    local playerName = script.Parent.Parent.playerNameTxtBox.Text
	    local cashValue = script.Parent.Parent.cashValueTxtBox.Text
	    local playerLoc = player.Name
	 
	    playerLoc.leaderstats.Cash.Value = cashValue
	end)

3 comments

0

Hey! Thanks for the reply but unfortunately I am still getting an error at line 5 ( 14:34:00.044 - Players.Playsourus.PlayerGui.adminMenu.adminGUI.adminFrame.setCash.setCash.LocalScript:5: attempt to index number with 'Name' 14:34:00.045 - Stack Begin 14:34:00.046 - Script 'Players.Playsourus.PlayerGui.adminMenu.adminGUI.adminFrame.setCash.setCash.LocalScript', Line 5 14:34:00.046 - Stack End)

0

i see.. So the problem is line 3 "local playerName = script.Parent.Parent.playerNameTxtBox.Text" cause you have written " local playerLoc = game.Players.playerName" So Fix that and it will work :)

0

Hey, made the changes, but if I understand the output correctly, the script is searching for a player named playerName not the string that the variable has. ( 14:48:54.997 - playerName is not a valid member of Players 14:48:54.997 - Stack Begin 14:48:54.997 - Script 'Players.Playsourus.PlayerGui.adminMenu.adminGUI.adminFrame.setCash.setCash.setCashScript', Line 5 14:48:54.997 - Stack End)

This discussion is preserved as it was. New answers, comments, and votes are closed.