Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Nidoxs 190
8 years ago

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

Log in to vote
1
Answered by 8 years ago

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.

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. Nidoxs 190 — 8y
Ad
Log in to vote
1
Answered by 8 years ago
--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)

Answer this question