I am making a game were you can throw potions at eatchother, different kinds have different effect. For Example, this script is meant to slow down a player on impact:
print("Slow" .. script.Parent.Name) for i = 1, 1 do local effect = script.Effect:Clone() effect.Parent = script.Parent.Torso effect.Enabled = true script.Parent.Humanoid.WalkSpeed = script.Parent.Humanoid.WalkSpeed - 10 wait(15) script.Parent.Torso.Effect:remove() script.Parent.Humanoid.WalkSpeed = script.Parent.Humanoid.WalkSpeed + 10 end script:remove()
That is the projectile script. How can I make a more effective script that effects the player, and works? also can penetrate tools and hats? That would be awesome, thanks! (It works on studio but not on a server)
This is only an example of what you could do. It's not a freebie :<
Well, for starters, you don't need a for loop for this. However, we are facing a problem. What if wherever the potion landed didn't have a torso?!?! :O ...Buuut it's a pretty simple fix. lol
The solution is to check if its a torso wherever the potion landed:
local hit = script.Parent if hit.Name:match('Torso') and hit.Parent:FindFirstChild('Humanoid') then print('Found teh torso') end
So far, now we've got something to check if the, ahem, wherever the potion landed was a torso, thus why using match to match the name. However, you probably noticed that there's a FindFirstChild function in the same conditional statement (the if
s in code). It's checking if the Humanoid
is there, and if it is, FirstFirstChild
returns true, otherwise, it'll return nil, meaning that there wasn't anything there.
The next step is to decrease speed, then return the player's speed to normal; However, there's a problem with your script. If the player were to be hit multiple times with potions, the speed value would be reversed! :O A simple solution, however, to counter this is a bit tricky, but you could create a tag, then when a potion hits the player, the potion wont affect.
Here's an example:
--Workspace ----Player ------Humanoid --------Tag --When that's the case, we would have the script check whether the tag is there or not. if not Humanoid:FindFirstChild('Tag') then print('No tag!') wait(10) end script:Destroy() -- If there's no tag found, the script'll destroy itself instead of waiting.
And that's pretty much what you'd do. :| Now, onto explanations for the junk mentioned! :D
Torsos
- The problem with checking whether a player has a specific torso is tricky because of the rigs, so just like I did with the example of match, I checked if the part has Torso
in the name.
For Loops - Because there's two different(?) types of for loops, I'm going to focus on the one in your code. A for loop iterates over a set number of times, like you set up yours to fire at least 1 time. There's many advantages to using this kind of for loop, so it's an ally in your quest! :D
FindFirstChild - In a nutshell: It checks if there's an object. Complicated: It's a function that checks if there's an object with a match name as the argument given, but it HAS to be exact, otherwise it wont detect it! :O Like I said before, if the object's there, it returns true & the object with it, and if not it returns nil, meaning that nothing was found.
Conditional Statements - In a nutshell: They check certain/specific conditions for comparing values, and if the conditions are met, it fires the next lines of code. >-> In other words, and scripter's best friend. :)
Match - Like explained earlier, match
checks a string if it has a match with a value it has been given, like Torso
in the above examples. This is VERY powerful in certain situations. :D
And that's pretty much it! :D Thanks for reading, and I hope this helped! :D