How To Get The Players Name From The PlayerService From A String?

So Basically, i dont know how to get the player name from the player service from a string or value

TycoonDoor = script.Parent

PlayerService = game:GetService("Players")

PlayerOwner = Instance.new("StringValue")
PlayerOwner.Parent = TycoonDoor

TycoonDoor.Touched:Connect(function(TycoonOwner)
	PlayerOwner.Value = TycoonOwner.Parent
	if PlayerOwner.Value == nil then
		print("worked")
	end
end)

Just do

local player = PlayerService:FindFirstChild(PlayerOwner.Value)

considering that PlayerOwner is a StringValue

i think i tried something like that, lemme try again

Touched returns the part that touched it. You need to figure out which character touched it by finding who owns the bodypart that touched your tycoon door, then use that to find the player.

TycoonDoor.Touched:Connect(function(part)
	if PlayerOwner.Value then-- Abort code if a player already owns this tycoon
		return
	end
	if not part.Parent:FindFirstChild("Humanoid") then-- This part isn't owned by a character-- abort code
		return
	end
	local TycoonOwner = PlayerService:GetPlayerFromCharacter(part.Parent)
	if not TycoonOwner then-- Character is not owned by a player (probably an NPC), abort code
		return
	end
	PlayerOwner.Value = TycoonOwner
	print("worked")
end)
1 Like

well that didnt work, i did try that but it did not print

local touchedPlayer = TycoonOwner.Parent:FindFirstChild("Humanoid") and PlayerService:GetPlayerFromCharacter(TycoonOwnner.Parent.Parent)

if not touchedPlayer then return end

PlayerOwner.Value = touchedPlayer.Name
1 Like

I missed that PlayerOwner is a stringvalue and not an objectvalue. Try this instead.

TycoonDoor = script.Parent

PlayerService = game:GetService("Players")

PlayerOwner = Instance.new("StringValue")
PlayerOwner.Parent = TycoonDoor

TycoonDoor.Touched:Connect(function(part)
	if PlayerOwner.Value ~= "" then-- Abort code if a player already owns this tycoon
		return
	end
	if not part.Parent:FindFirstChild("Humanoid") then-- This part isn't owned by a character-- abort code
		return
	end
	local TycoonOwner = PlayerService:GetPlayerFromCharacter(part.Parent)
	if not TycoonOwner then-- Character is not owned by a player (probably an NPC), abort code
		return
	end
	PlayerOwner.Value = TycoonOwner.Name
end)
1 Like

i changed it back to stringvalue because i was looking around the forums that a string value can not reference or access a 3d object, then i realized that was false so i changed back to stringvalue and removed objectvalue

1 Like

Okay then you should be able to use the old code I sent, I tested it

1 Like

thanks, one question, why isnt it as simple as only 2 lines of code for an if statement

1 Like

You can if you want, it just makes breaking up comments worse and makes it harder to read long text in the devforum’s little codebox thingy
Edit: Also you probably just want to use an object value for this use case. ObjectValues just directly point to the player and can be used in if checks all the same. In fact you can get the same result as a string value simply by doing PlayerOwner.Value.Name.

1 Like

but i cant if i want tho, nothing prints out whenever i do

TycoonDoor = script.Parent

PlayerService = game:GetService("Players")

PlayerOwner = Instance.new("StringValue")
PlayerOwner.Parent = TycoonDoor

TycoonDoor.Touched:Connect(function(TycoonOwner)
	PlayerOwner.Value = TycoonOwner.Parent
	if PlayerOwner.Value == PlayerService:FindFirstChild(PlayerOwner.Value) then
		print("worked")
	end
end)

but somehow your code works?

There’s only one if case in here while the example code uses 3? Doesn’t seem like you’re using these compound if checks correctly. I’ll quickly make it all one line to show you how it should be looking

Edit:

TycoonDoor = script.Parent

PlayerService = game:GetService("Players")

PlayerOwner = Instance.new("ObjectValue")
PlayerOwner.Parent = TycoonDoor

TycoonDoor.Touched:Connect(function(part)
	if PlayerOwner.Value or not part.Parent:FindFirstChild("Humanoid") then
		-- Check #1: Abort code if a player already owns this tycoon
		-- Check #2: Abort code if it isn't owned by a character
		return
	end
	local TycoonOwner = PlayerService:GetPlayerFromCharacter(part.Parent)-- We need a reference to this value after the if check, hense why we're not getting it in the if check above us.
	if not TycoonOwner then-- Character is not owned by a player (probably an NPC), abort code
		return
	end
	PlayerOwner.Value = TycoonOwner
	print("Worked")
end)

Again this is using objectvalue rather than stringvalue because objectvalues in this use case are more versatile than using a stringvalue.

1 Like

hmm it seems i misunderstood the GetPlayerFromCharacter

TycoonDoor = script.Parent

PlayerService = game:GetService("Players")

PlayerOwner = Instance.new("ObjectValue")
PlayerOwner.Parent = TycoonDoor

TycoonDoor.Touched:Connect(function(TycoonOwner)
	PlayerOwner.Value = TycoonOwner.Parent
	local Player = PlayerOwner.Value
	if PlayerService:GetPlayerFromCharacter(Player) then
		print("worked")
	end
end)

You’re gonna get errors without the other if checks depending on what touches your tycoon door & also without the first if check other players can steal your tycoon by just touching the tycoon door again. Not sure if that’s intended behavior.

1 Like