Get player by touched function

does anyone know how to get the player inside of this script? it is in the workspace

( the player is not touching the part) but it’s another part touching it

script.Parent.Touched:Connect(function(hit)
	local ls = plr:WaitForChild("leaderstats")
	ls.Money.Value = ls.Money.Value + hit.Parent.value.Value
end)
1 Like
game.Players:FindFirstChild(hit.Parent.Name)

Returns the player.

1 Like

( the player is not touching the part) but it’s another part touching it

Also, if you were to do this, you would have to check if hit is a part of a character.

So why are you getting the player?

because i need the leaderstats inside the player

so are you saying the part will magically be like the player but it’s not, and it’s controlled by the player?

If so add a string value in the parr, and get the player from the string.

nope i am making a tycoon and when the part1 touch the sell Part2 the value inside plr leaderstats should increase by the value inside the Part1

the problem is thet i first need the player to get the leaderstats.

Ohhhh, so you would need to add a string value to that part which is touching it then

game.Players:FindFirstChild(hit.Parent.owner.Value)

it is a server script and localPlayer work only on localScripts

He’s probably using a server script.

so you would need to add a string value to that part which is touching it then

game.Players:FindFirstChild(hit.Parent.ownerstring.Value)

ok i am going to try right now

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local part = Instance.new("Part")
		part.Name = player.Name.."Part"
		part.Parent = workspace
		
		local object = Instance.new("ObjectValue")
		object.Name = player.Name.."Value"
		object.Parent = part
		object.Value = character
		
		local money = Instance.new("IntValue")
		money.Name = "Money"
		money.Parent = part
		money.Value = 100
	end)
	
	player.CharacterRemoving:Connect(function(character)
		workspace:FindFirstChild(player.Name.."Part"):Destroy()
	end)
	
	script.Parent.Touched:Connect(function(hit)
		if hit:FindFirstChild(player.Name.."Value") then
			local ls = player.leaderstats
			local money = ls.Money
			money.Value += hit.Money.Value
		end
	end)
end)

This will need to be changed slightly to fit your purposes.

1 Like

I understand it has been a long time since this post was originally made but I may have seemed to have found a solution.


Server script:

script.Parent.Touched:Connect(function(Hit)
    local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)

    if Player then
       Player.leaderstats.Money.Value += 10 -- You can replace the number 10 with whatever value you want to reward the player with
    end
end)

Wow… almost 2 years since this was posted.
I dont think OP still need the answer. Your reply is more like the “mandatory procedure” to follow for a touch event, maybe it was not needed to rebump this, but yeah why not :yum:

Yeah it probably wasn’t… ah well… the only reason I posted this was because the topic isn’t yet closed as there is currently no solution yet.

1 Like

Maybe OP forgot about it. Theres a lot of unsolved topics. Mine still unsolved and I need some suggestions! could you? :melting_face: :upside_down_face: