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

how to find a player from a string value?

ok so im a beginner scripter and im trying to make a single player tycoon. right now im trying to make a script that adds cash directly from ore to player . i was brainstorming all day how to make it work.

local furnace = script.Parent
wait(1)
local plrname = game.Workspace.PlayerName.Value
furnace.Touched:Connect(function(hit)
	if hit.Cash ~= nil then
		local player = game.Players.plrname
		multiplier = script.Parent.Parent.Multiplier.Value
		wait(0.1)
		hit.BrickColor = BrickColor.new("Dark stone grey")
		wait(0.1)
		hit.BrickColor = BrickColor.new("Really black")
		wait(0.1)
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + hit.Cash.Value * multiplier
		hit:Destroy()
	end
end)

plrname is the stringvalue which shows player name

error im getting: 11:07:12.722 - plrname is not a valid member of Players

does anyone know how to fix it?

3 comments

0

On line 6, do "local player = game.Players:FindFirstChild(plrname)"

0

You can also use game.Players[plrname]

0

uh u made this script before in the 2nd page i saw

3 answers

✓ Accepted answer

Consider FindFirstChild() for your situation.

Instance:FindFirstChild() has two parameters:

  1. The name of the child to look for (string)

  2. Recursive searching (boolean). If set to true, FindFirstChild() will search recursively, meaning that it will search through all descendants of Instance to find the given child. (This parameter is optional and is set to false by default.)

FindFirstChild() will do one of two things:

  1. If a child of given name is found, FindFirstChild() returns the userdata with that name.

  2. If no child of given name is found, FindFirstChild() returns nil.

As a safety net, line 5 should be:

if hit:FindFirstChild("Cash") then

Then you'd change line 6:

local player = game.Players:FindFirstChild(plrname)

Since plrname is a string, it would only make logical sense to use a method that takes a string.

Better yet, if a player touches the part, you can acquire it using GetPlayerFromCharacter():

if game.Players:GetPlayerFromCharacter(hit.Parent) then

If the condition passes, it means that a player touched the part.

1 comment

0

It's not the player who should touch it, imagine a tycoon, small parts will touch something and then be destroyed, this script, is in the part they touch, at the end of the conveyor

Simple fix.

game.Workspace.PlayerName.Value is what is breaking your script. You want to change it to

local plrname = game.Players.LocalPlayer.Name

This is because the "players" group is outside of "workspace".

3 comments

0

yeh but its not a local script

0

doesnt work

0

even if i change the script to local script

Heres a tip im not guranteed itll work but based on what u described all u really need to know is the owner of the tycoon right? so first u should make the tycoon door owner and when a player touches it do this script like the touch to be owner doors they usually make

script.Parent.Touched:connect(function(part)
local player = Part.parent.parent

-- this will tell the game the players name wont work if the character has a hat or accesory if he does u can do
-- these 3 lines the local player and if not is optional u can either use the one above or this if players have hats or accesorys i recommend the one above as this may not work
local player = part.parent.parent
if not player then
local player = part.parent.parent.parent
script.Parent.Transparency = 0.5
script.Parent.CanCollide = false
-- then for the other script u will do this like when the ore touches the pad btw put this all in a single script
end
end)
game.workspace.(OreName).Touched:connect(function(part)
if part = game.workspace.(CashPadName) then
local multiplier = script.Parent.Parent.Multiplier.Value
wait (0.1)
hit.Brickcolor = Brickcolor.new("Dark stone grey")
wait (0.1)
hit.Brickcolor = Brickcolor.new("Really Black")
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + part.Cash.Value * multiplier
part:Destroy()
end
end)
-- this should be local script btw and  also change the orename and cashpadname into the thing and put the script in the area i hope this helped

2 comments

0

i explained it all but listen in the beginning script if the majority of players dont have accesorys u can delete the if not player one and just use the regular one k?

0

also in the beginning script i put script.Parent as u should put this in the owner door but u can just select the owner door by a different if u put the script in a different location then in the touch to be owner door

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