I have a script that enables a particle emitter on the player's torso when it touches this block. It's for an invincibility-effect type thing. But I come across an error while trying to make it work.
13:00:33.055 - GetPlayerFromCharacter is not a valid member of DataModel 13:00:33.056 - Script 'Workspace.DashPanel.DashPart.ParticleScript', Line 3
Here's the script:
function onTouched(hit) if workspace:findFirstChild(hit.Parent.Name) then local players = game:GetPlayerFromCharacter(hit.Parent) workspace.player.Torso.DashParticle.Enabled = true wait(1) workspace.player.Torso.DashParticle.Transparency = NumberSequence.new(.4) wait(1) workspace.player.Torso.DashParticle.Enabled = false workspace.player.Torso.DashParticle.Transparency = NumberSequence.new(0) end end script.Parent.Touched:connect(onTouched)
I figure the way to fix this is to get the script to insert the name of the player to replace the placeholder in lines like workspace.player.Torso.DashParticle.Enabled, but there's probably other solutions to stop getting this error that I'm open to. Thanks in advance!
Lesson time~!
Using workspace.player
is going to look for a model/part/whatever named player in the workspace. So you have to use a different type of method.
For example, you defined hit when you used your function, so that will end up defining anything that touches that brick, whether it be the baseplate or a player, it will call it hit. That's why whenever you are using hit, you want to make sure to ask the script if there is a Humanoid, a basic object for players and NPCs, inside what touched it.
There are different ways of doing this.
I personally use if hit.Parent:FindFirstChild('Humanoid') then
It will check to see if the hit's parent (meaning if your left leg or right arm hit the object, it would go higher up, to the character, and check if there is a humanoid in it.)
After you use the if statement, you could throw all your code into it. I'll go ahead and script you up a better looking script so that you can use that, I'll throw in some comments along the way, since I can tell you're new to scripting.
script.Parent.Touched:connect(function(hit) --I used this instead of naming the function and calling it later because I find this to be faster and better then the other way. if hit.Parent:FindFirstChild('Humanoid') then --Checks for a humanoid character = hit.Parent --Defines the hit.Parent, which has been checked for a Humanoid, character. character:WaitForChild('Torso').DashParticle.Enabled = true --Changed workspace.player to character. wait(1) character.Torso.DashParticle.Transparency = .4 --You used a comma instead of a dot, I correct it ^-^ wait(1) character.Torso.DashParticle.Enabled = false character.Torso.DashParticle.Transparency = 0 end end)