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

How to check if a player is touching a specific part? If so continue code?

Player presses key, if touching a certain part with a certain name then continue code, how would I do this?

--doctorbenny2011 09/04/2016 15:45GMT

istouching = false
local Player = game.Players.LocalPlayer
repeat wait(.01) until Player.Character
local Mouse = Player:GetMouse()
local Animated = false

Player.Character["Left Leg"].Touched:connect(function(hit)
    if hit then
		if hit.Name == "Matress" then
        istouching = true  
		elseif hit.Name ~= "Matress" then
		istouching = false
		end
    end
end)

Mouse.KeyDown:connect(function(key)
    local Animation = Instance.new("Animation")
    if key == "c" then
        if istouching == true then
           game.Workspace.BasePlate.CFrame = game.Workspace.BasePlate.CFrame * CFrame.Angles(0,1,0)
        end
    end
end)


2 answers

You might be able to use Touch and TouchEnded.

tv = 0
function pint()
	print ("touch"..tv)
	tv = tv +1
end
function upint()
	print "touch ended"
end
script.Parent.Touched:connect(pint)
script.Parent.Touched:connect(upint)

something like that should work. It won't work very well, but it will work.

1 comment

0

But this local script will be inside the PlayerGui, I'm trying to check if the player is touching a part with a specific name FROM the players PlayerGui.

--doctorbenny2011 09/04/2016 15:45GMT

istouching = false
local Player = game.Players.LocalPlayer
repeat wait(.01) until Player.Character
local Mouse = Player:GetMouse()
local Animated = false

--UserInputFunction
Matress.Touched:connect(function(touched) --Detects when touched & define matress
	if touched.Parent.Humanoid == true then
		istouching = true	
	end
end

Matress.TouchedEnded:connect(function() -- Detects when touched ended
	istouching = false
end

Mouse.KeyDown:connect(function(key)
    local Animation = Instance.new("Animation")
    if key == "c" then
        if istouching == true then
            	--Code
        end
    end
end)

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