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

How can i change a Touched script into a Clicked script?

Asked by 8 years ago
local ting = 0

function onTouch(hit) 
    if ting == 0 then
        ting = 1
        local check = hit.Parent:FindFirstChild("Humanoid")
        if check ~= nil then
            local user = game.Players:GetPlayerFromCharacter(hit.Parent)
            print("player got found")
        end
    end
    ting = 0
end

script.Parent.Touched:connect(onTouch)

I just would like to know how to change it to a Clicked script but still work.

0
Use a click detector. Operation_Meme 890 — 8y

1 answer

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
8 years ago

It's actually a whole lot easier than Touched. First off, you need to insert a ClickDetector into the Part, along with the script. We will use the MouseClick event of the ClickDetector to detect when someone clicks.

The part of this that makes it so much easier is the built in parameters. Touched's built in parameter is the part that fired the event, so it might be equal to your left leg or something. But because anything can fire the event, you have to make sure the Humanoid exists, like you did on line 7. You then have you use GetPlayerFromCharacter to be able to use the player.

This isn't necessary for the MouseClick event. Its built in parameter is equal to the Player who clicked. This makes all those checks we did for Touched not needed. Check it out;

function onClick(player)
    print(player.Name.." was found!")
end

script.Parent.ClickDetector.MouseClick:connect(onClick)
0
Wow Thanks SamTheDeveloper 85 — 8y
Ad

Answer this question