.touched event doesnt work in local script

  1. What do you want to achieve? I want this script to do: if the players character has touched the exact part with that name it changes string value to the players name.

  2. What is the issue? I cant trigger .touched event.

I have tried typing in different parts of character to see if they can trigger event.

No errors in output

local players = game:GetService("Players")
local Player = game.Players.LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait() 
local button = script.Parent
local playerv = script.Parent.Value
local foot = character:FindFirstChild("RightFoot")

foot.Touched:Connect(function(hit)
	print("tach")
	if hit.Name == "Buttonlvl1" then
		local player = players:GetPlayerFromCharacter()
		playerv.Value = player.Name
		print("Owner set to "..player.Name)
	end
end)
1 Like

Your script looks fine. Just make sure that if you’re using R6, do this instead:

Script
local players = game:GetService("Players")
local Player = game.Players.LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait() 
local button = script.Parent
local playerv = script.Parent.Value
local foot = character:FindFirstChild("RightLeg")

foot.Touched:Connect(function(hit)
	print("tach")
	if hit.Name == "Buttonlvl1" then
		local player = players:GetPlayerFromCharacter()
		playerv.Value = player.Name
		print("Owner set to "..player.Name)
	end
end)
Script 2
local players = game:GetService("Players")
local Player = game.Players.LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait() 
local button = script.Parent
local playerv = script.Parent.Value
local foot = character:WaitForChild("RightLeg")

foot.Touched:Connect(function(hit)
	print("tach")
	if hit.Name == "Buttonlvl1" then
		local player = players:GetPlayerFromCharacter()
		playerv.Value = player.Name
		print("Owner set to "..player.Name)
	end
end)

Also, make sure to check that the object you’re looking for is actually named "Buttonlvl1"

As @oddcraft18 said the character could be r6 instead of r15. Also, if you are using :GetPlayerFromCharacter(), you must assign a value (Character) in parentheses as its name. Script:

local players = game:GetService("Players")
local Player = game.Players.LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait() 
local button = script.Parent
local playerv = script.Parent.Value
local foot = character:FindFirstChild("RightFoot") or character:FindFirstChild("RightLeg")

foot.Touched:Connect(function(hit)
	print("tach")
	if hit.Name == "Buttonlvl1" then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		playerv.Value = player.Name
		print("Owner set to "..player.Name)
	end
end)

Thank you!
I didnt know you have to put a value in GetPlayerFromCharacter
Also i remade the script so that .touched now works and its no longer in local script

local players = game:GetService("Players")
local button = script.Parent
local playerv = script.Parent.Value
local ownerset = 0

button.Touched:Connect(function(hit)
	if hit.Name == "RightLowerLeg" and ownerset == 0 then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		playerv.Value = player.Name
		print("Owner set to "..player.Name)
		ownerset = 1
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.