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

What did I do wrong that doesn't make the part CanCollide?

Asked by 9 years ago

I am just making a random script from what I know basically. I am making a part, which I named ScriptedPart, to where when I hit G it CanCollides, and if I hit H, it like goes back to normal. This is what I put so far:

local part = game.Workspace.ScriptedPart

function part(OnTouched)    
        if part.CanCollide == Enum.KeyCode.G then
            part.CanCollide = false
        if part.CanCollide == Enum.KeyCode.H then
            part.CanCollide = true
        end
    end
end

I hope I gave a clear explanation.

2 answers

Log in to vote
1
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

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")
-- Use a LocalScript.

function ChangeCollision(InputObject)
    -- An "InputObject" is returned from here.
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
-- This makes sure that YOU'RE the only one that can do it
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)
0
Thanks man. It worked really good. :D RobotChitti 167 — 9y
0
You're welcome. :) Redbullusa 1580 — 9y
Ad
Log in to vote
2
Answered by
Relatch 550 Moderation Voter
9 years ago

You're problem is is that it never calls the touched function. To fix this, just call the touched function, or try using part.Touched:connect(function(), and changing your end to end).

CODE:

local part = game.Workspace.ScriptedPart

part.Touched:connect(function() 
        if part.CanCollide == Enum.KeyCode.G then
            part.CanCollide = false
        if part.CanCollide == Enum.KeyCode.H then
            part.CanCollide = true
        end
    end
end)

OR:

local part = game.Workspace.ScriptedPart

function part(OnTouched)    
        if part.CanCollide == Enum.KeyCode.G then
            part.CanCollide = false
        if part.CanCollide == Enum.KeyCode.H then
            part.CanCollide = true
        end
    end
end

part.Touched:connect(part)
0
May want to take another look at the codes: Lines 4 and 6. :) TheeDeathCaster 2368 — 9y
0
right.. Relatch 550 — 9y
0
I will upvote even though it didn't work. But thanks for helping me. :D RobotChitti 167 — 9y

Answer this question