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

How to make a see if a player has touched a part?

alright so script clones Local Part inside workspace. The script gets the player, but how to check if the part has been hit by the player?

local Coins = workspace.Coins:GetChildren()
local player = game.Players.LocalPlayer
print(player)
 for i, v in pairs(Coins) do
     if v.Name == "CoinGet" then
         v.Touched:Connect(function(hit)
             if hit.Parent:FindFirstChild("Humanoid") and 
   game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
            game.ReplicatedStorage.Event:Fire()
          v:Destroy()
         end
       end)
    end
end

If this is confusing then imagine this

player = Player1

script.parent.touched:connect(function()
-- checks if Player1 touched the part
print("a")
end)

How can you check if the given player has touched the part? If player1 touched the part it will print a, but if player 2 or any other player toches the part it wont do anything

1 comment

0

Hi again. Could I ask why you unaccepted my answer?

2 answers

✓ Accepted answer

You can get the Character who touched it from "hit"

script.Parent.Touched:Connect(function(hit)
	if hit.Parent.Name == "Player1" then
		print("A")
	else
		print("Not Player1!")
	end
end)

You could also use game.Players:GetPlayerFromCharacter(hit.Parent) to get the Player who touched it.

1 comment

0

Sorry, It didn't work like I needed it to. I got the player like this local player = game.Players.LocalPlayer and then Tried doing this if hit.Parent.Name == player then, but it didnt work

here:

script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		print("Player "..hit.Parent.Name.." touched "..script.Parent.Name..".")
	end
end)

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