✓ Accepted answer
That's not how you use UserInputService!
To use it, since it's a service, direct an InputBegan function from it.
UIS = game:GetService("UserInputService")
function ChangeCollision(InputObject)
end
UIS.InputBegan:connect(ChangeCollision)
An InputObject has KeyCode as its member, so you can (from there) interpret whether the key pressed is the same as the key you intend the players to press (such as "G" or "H" in your case).
UIS = game:GetService("UserInputService")
function ChangeCollision(InputObject)
if InputObject.KeyCode == Enum.KeyCode.G then
elseif InputObject.KeyCode == Enum.KeyCode.H then
end
end
UIS.InputBegan:connect(ChangeCollision)
Now you can do the rest!
UIS = game:GetService("UserInputService")
Part = workspace.ScriptedPart
function ChangeCollision(InputObject)
if InputObject.KeyCode == Enum.KeyCode.G then
Part.CanCollide = false
elseif InputObject.KeyCode == Enum.KeyCode.H then
Part.CanCollide = true
end
end
UIS.InputBegan:connect(ChangeCollision)
NOTE: If you don't want your function to be fired while you're chatting, click here!
If you want to make your part activate the function along with the .Touch event, then use the .Touch event to, in a sense, "activate" the .InputBegan event.
Keep in mind that the .Touch event is a super sensitive and touchy (haha) one. So it might glitch.
Player = game.Players.LocalPlayer
UIS = game:GetService("UserInputService")
Part = workspace.ScriptedPart
CanChangeCollision = false
function ChangeCollision(InputObject)
if CanChangeCollision then
if InputObject.KeyCode == Enum.KeyCode.G then
Part.CanCollide = false
elseif InputObject.KeyCode == Enum.KeyCode.H then
Part.CanCollide = true
end
end
end
function Switch(Boolean)
if Boolean then
CanChangeCollision = true
else
CanChangeCollision = false
end
end
function VerifyHit(hit)
HumanoidIsFound = hit.Parent and hit.Parent:FindFirstChild("Humanoid")
PlayerIsFound = game.Players:GetPlayerFromCharacter(hit.Parent)
if HumanoidIsFound and HumanoidIsFound.Health > 0 and PlayerIsFound and PlayerIsFound == Player then
return true
else
return false
end
end
Part.Touched:connect(function(hit)
if VerifyHit(hit) then
Switch(true)
end
end)
Part.TouchEnded:connect(function(hit)
if VerifyHit(hit) then
Switch(false)
end
end)
UIS.InputBegan:connect(ChangeCollision)
2 comments
Thanks man. It worked really good. :D
You're welcome. :)