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

How do i used a touched function?

Asked by
KelcriC -1
6 years ago
    script.Parent.Touched:Connect(function(player)
        local part = script.Parent

        part.CFrame = part.CFrame + Vector3.new(0, -8, 0)
        player.leaderstats.Power.Value = player.leaderstats.Power.Value + 4

        wait(120)

        part.CFrame = part.CFrame + Vector3.new(0, 8, 0)

        end)

In the code it says that player is not a valid member of meshModel.

0
First off, it's not a function, it's an event, and touched returns the part that touched the part handling the event Rare_tendo 3000 — 6y

4 answers

Log in to vote
0
Answered by
Ince_FS 13
6 years ago
Edited 6 years ago

Use this for that. Btw, the event "Touched" returns the part touched, not the player.

--<<Example of this function]

workspace.Part.Touched:Connect(function(plr)
local Player = game:GetService("Players"):GetPlayerFromCharacter(plr.Parent);
    if Player then
        print(Player.Name)
    end;
end);

--<<Example of this function]
0
Events cannot return anything. piRadians 297 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

When a part is touched, another part will have touched that part. The Touched event will give the part that touched it as the parameter of the function that is connected to the event. It won't find the player on its own. Luckily, there is a method called "GetPlayerFromCharacter", from the Players service. As the name implies, it will return the player object from the character. When a player touches a part, it will be a character's bodypart, their leg, arm, etc. The Parent of the limb (or whatever touched it) would be the character.

local Part = script.Parent

Part.Touched:Connect(function(part)
    -- Name ir anything. I decided to just name it 'part'.
    local player = game.Players:GetPlayerFromCharacter(part.Parent)

    if player then -- If it exists...
        Part.CFrame = Part.CFrame + Vector3.new(0, -8, 0)
        player.leaderstats.Power.Value = player.leaderstats.Power.Value + 4
        wait(120)
        Part.CFrame = Part.CFrame + Vector3.new(0, 8, 0)
    end
end)

Hopefully this answered your question and if it did don't forget to hit that accept button. If you have any other questions then feel free to ask them in the comments below.
0
It works, but instead of moving the part, it moves the player KelcriC -1 — 6y
0
It moves the player KelcriC -1 — 6y
0
whoops my bad User#19524 175 — 6y
Log in to vote
0
Answered by
piRadians 297 Moderation Voter
6 years ago
Edited 6 years ago

Events

Touched is NOT a function, it is an event. When a function is called alongside it, however, the first argument is the part who touched, NOT the Player.

Ancestry

As I said before, the first argument to the function is the part who touched the part handling the event, meaning if a character touched it, you can reference them.

Practice

You would have to use if statements, FindFirstChild method, and the GetPlayerFromCharacter method.

Respectively, these are used to:

  • Check if a condition is met.

  • Check if a Child exists in a Parent object.

  • Retrieves the player from a character.

script.Parent.Touched:Connect(function(hit)

    local part = script.Parent

    part.CFrame = part.CFrame + Vector3.new(0, -8, 0)
    player.leaderstats.Power.Value = player.leaderstats.Power.Value + 4
    if hit.Parent:FindFirstChild("Humanoid") then -- checks if a humanoid is present, otherwise your script will break
        plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- The parent will be the character
    if plr then -- checks if the player is valid, for in a case where an NPC touches the part, it would also break the script.
            plr.leaderstats.Power.Value = player.leaderstats.Power.Value + 4

    end
    wait(120)

    part.CFrame = part.CFrame + Vector3.new(0, 8, 0)

    end)


Log in to vote
0
Answered by
Cyrakohl 108
6 years ago

The touched event returns what touched it if you want to get the player from the thing that’s said touch then use GetPlayerFromCharacter() More info here: https://www.robloxdev.com/api-reference/function/Players/GetPlayerFromCharacter

Answer this question