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.
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]
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)
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.
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.
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)
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