Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do i used a touched function?

	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.

2 comments

0

First off, it's not a function, it's an event, and touched returns the part that touched the part handling the event

4 answers

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]

1 comment

0

Events cannot return anything.

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.

3 comments

0

It works, but instead of moving the part, it moves the player

0

It moves the player

0

whoops my bad

#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)


This discussion is preserved as it was. New answers, comments, and votes are closed.