Trying to get player from part.Touched script

As you can see the part below touches the plate and I’m trying to get the local player when it does. All I want it to do is tell me the name of the local player. From there I can find leaderstats and increase whatever value I want but I can’t even find the player. I can get it to tell me the name “Players” but I’ve been trying for an hour and since the player isn’t the one that touched the plate I can’t seem to get it right. The script is in the part since it’s being cloned I figured it was the easiest way.

Nothing prints when the part touches the plate (TouchPart). I’ve managed to get so many different errors while mucking around with it from “= nil, to waitforchild etc…”.

image

local goldPart = script.Parent

goldPart.Touched:Connect(function(hit)
	if hit:IsA("Part") and hit.Name == "TouchPart" then
		game.Players.PlayerAdded:Connect(function(player)
			print(player.Name)			
		end)
		--print("Part is touched")
		wait(0.5)
		goldPart:Destroy()
	end
end)
1 Like

use :GetPlayerFromCharacter(hit.Parent) to get the player of a character

local goldPart = script.Parent

goldPart.Touched:Connect(function(hit)
	if hit:IsA("Part") and hit.Name == "TouchPart" then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		wait(0.5)
		goldPart:Destroy()

		--player.leaderstats.Gold.Value += 1
	end
end)
1 Like

What will that do since the player doesn’t “hit” the plate?

I am assuming that there is a part in the player named TouchPart but nvm

local goldPart = script.Parent

goldPart.Touched:Connect(function(hit)
	if hit:IsA("BasePart") then -- BasePart for accessories, Part for limbs ( I think )
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)

		if player then -- player is found
			wait(0.5)
			
			print(player.Name)
			goldPart:Destroy()
		end
	end
end)

Umm, no the player isn’t what touches the plate. The part is spawned as shown in the picture and touches the plate at the end of the tycoon. When it touches the plate it runs the touch function. Within the touch function I need to get the player (so I can give the player gold or whatever). The player doesn’t touch anything.

There are many free models of tycoons (It looks like you’re making one).
With collector / scripts in it.
Maybe try looking at the scripts inside those models / the collector model and base your script off of it.

You can add a value in the TouchPart which stores the player’s name and do

local goldPart = script.Parent

goldPart.Touched:Connect(function(hit)
	if hit:IsA("Part") and hit.Name == "TouchPart" then
		local player = game.Players:FindFirstChild(hit:FindFirstChildWhichIsA("Value").Value)
		
		wait(0.5)
		goldPart:Destroy()

		--player.leaderstats.Gold.Value += 1
	end
1 Like

Uh, the script is kind of messed up in my opinion.

You can just do like:

You can basically just use :GetPlayerFromCharacter(), plus if you want to check if a player hit it, it’s hit.Parent.

local goldPart = script.Parent

goldPart.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		print(player.Name .. " hit the part")
	end
end)

image

1 Like

@Workindad01 said the player is not touching the plate & the part touching the plate is not parented to the player’s character either.

Actually yeah I’m probably going to do something like that. I identified the problem as because two parts touched instead of a player then I need to tell the server which player’s part touched it. This means I need to somehow connect the server to the client and back again. I can’t make this part local as in a game a local tycoon would mean no player interaction and your base would be blank area to other players. So I think I’m getting ahead of myself.

In a tycoon the player touches a gate and somehow the entire tycoon becomes attached to the player (idk how but that what it does) and I suppose the parts-that-spawn are also attached to the player by maybe a string, value or something which can be read by the parts that touch the collector. I’m assuming some kind of remote event tells the server that this player owns the tycoon and then each part can just refer to that string or value when it’s collected and the currency added to their wallet.

Do I understand the problem? The fact that I can’t get the player is because the part is on the server and needs to know which player’s it is before it can do anything. At the moment the part cannot find the player because it’s owned (for want of a better word) by the server. I’m guessing a remote event that connects the player (maybe a value as you suggested) to the part but I’m guessing that’s impossible unless the player actually placed the turret.

Once again, it’s not a player touch script rather one part touching another. Everyone is so used to seeing (function(hit)) for a player touch script they just assume it’s for a player. If 2 parts touch each other what can I use instead to ease confusion of (hit). Maybe (isTouching)? What do you suggest I use instead?

Server Script:
Make A BindableFunction Named: playerData

game.Players.PlayerAdded:Connect(function(plr)
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local playerData = ReplicatedStorage:WaitForChild("playerData")
	local InvokedPlayer = playerData:Invoke(plr)
end)

Part Script:

goldPart.Touched:Connect(function(hit)
	if hit:IsA("Part") and hit.Name == "TouchPart" then
		local ReplicatedStorage = game:GetService("ReplicatedStorage")
		local playerData = ReplicatedStorage:WaitForChild("playerData")
		playerData.OnInvoke = function(plr)
			plr.leaderstats.SomeLeaderstats.Value += 1
		end
		wait(0.5)
		goldPart:Destroy()
	end
end)
1 Like

Oh Yeah plr.leaderstats.SomeLeaderstats.Value += 1 Name .SomeLeaderstats to Your leaderstats value name

I’m very new to programming and I need help.
Thanks in advance for any help.

If you are looking for the player who touched the particle and then you want to give them something, try this.
local goldPart = script.Parent

goldPart.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
– Do something here
end
end)

However, if you are looking to give the player who touched the particle a tool that they can use, you will have to put the tool in their backpack, or put the tool in their character.
local goldPart = script.Parent

goldPart.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local tool = Instance.new(“Tool”)
tool.Parent = player.Backpack
end
end)