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

How to do i get players name using return?

So i'm trying to get the name of the player without using any tables or anything, just a return. I know this script has errors and is wrong, but it's mostly because i don't understand return that much.

local Part = script.Parent
local db = false
function IdentifyPlayer(hit,plr)
    if db == false then
		db = true
		local player = hit.Parent
		if player:FindFirstChild("Humanoid")then
			return player
		end
		db = false
	end
end
ThePlayer = IdentifyPlayer()--idk what to put on here
Part.Touched:Connect(IdentifyPlayer)

3 comments

0

Since you're connecting the function, it has nowhere to return to. Return will give back a value to whatever called the function. You also have an unused parameter called "plr" since the Touched event only provides one argument to connected functions. You might want to check out https://developer.roblox.com/api-reference/function/Players/GetPlayerFromCharacter

0

return player.Name

1 answer

✓ Accepted answer

There is always a way around things like this. When you script, there are tons of way to do it. In this case I suggest a function mainly to identify the player.

If you actually want the Player object

local Part = script.Parent

local function getPlayer(Char)
	if game:GetService('Players'):GetPlayerFromCharacter(Char) then
		return game:GetService('Players'):GetPlayerFromCharacter(Char)
		else
		return nil
	end
end

local Debounce = true

Part.Touched:Connect(function(Hit)
    if not Debounce then return end
	if Hit.Parent:FindFirstChild('Humanoid') then
    	Debounce = false
		local Player = getPlayer(Char)
		-- Do stuff
		wait()
		Debounce = true
	end
end)

So basically, you first check if there is a Humanoid to the Hit. Next check for a humanoid object and if there is then check if it's a player. If it's not a player Player will be nil. If it is a player it will return the player.

Hopefully this helped! Basically you can split up your code

Best of luck developer!

BlackOrange3343

PS: If you want the name, change return game:GetService('Players'):GetPlayerFromCharacter(Char) to return game:GetService('Players'):GetPlayerFromCharacter(Char).Name OR replace --Do stuff with local PlayerName = Player.Name. I returned the full player because it gives you more properties to work with.

1 comment

0

stop it with your retarded intro i swear

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