There are two parts of the script, the first part has LocalPlayer, which I just heard that it only works in studio? but this is only method of coding the animation to play that I know.
01 | player = game.Players.LocalPlayer |
02 | repeat wait() until player.Character |
03 | character = player.Character |
04 | CanAttack = true |
05 | animation = character.Humanoid:LoadAnimation(script.Parent.AttackAnim) |
06 | script.Parent.Activated:connect( function () |
07 | if CanAttack = = true then |
08 | CanAttack = false |
09 | script.Parent.Attack.Value = true |
10 | animation:Play() |
11 | wait( 0.5 ) |
12 | CanAttack = true |
13 | script.Parent.Attack.Value = false |
14 | end |
15 | end ) |
The second part is just for damage, and seems to work fine.
01 | wait( 1 ) |
02 | CanAttack = true |
03 | script.Parent.Touched:connect( function (part) |
04 | local human = part.Parent:findFirstChild( "Humanoid" ) |
05 | if human then |
06 | if human.Parent.Name ~ = script.Parent.Parent.Parent.Name and |
07 | CanAttack = = true and script.Parent.Parent.Attack.Value = = true then |
08 | CanAttack = false |
09 | human:TakeDamage( 10 ) |
10 | wait( 0.5 ) |
11 | CanAttack = true |
12 | end |
13 | end |
14 | end ) |
Any help on this would be awesome. Thanks!
Okie time to help,
I will tell you all the problems
First part (Script 1)
01 | -- in local scripts when using variables always use local and WaitForChild allows the system find the variable quicker |
02 | local player = game.Players.LocalPlayer -- added local |
03 | repeat wait() until player.Character |
04 | local character = player.Character -- added local |
05 | local CanAttack = true -- added local |
06 | local animation = character.Humanoid:LoadAnimation(script.Parent.AttackAnim) -- added local |
07 | local tool = script.Parent -- a tool |
08 | tool.Activated:connect( function () --with the new variable we change script.Parent with tool |
09 | if CanAttack = = true then |
10 | wait( 0.2 ) -- let the system calm down |
11 | CanAttack = false |
12 | tool:WaitForChild( "Attack" ).Value = true -- use tool |
13 | wait( 0.1 ) -- let the system cool |
14 | animation:Play() |
15 | wait( 0.5 ) |
16 | CanAttack = true |
17 | tool:WaitForChild( "Attack" ).Value = false -- use the variable tool and add WaitForChild |
18 | end |
19 | end ) |
there, all the green is what the changes are
NOTE: IF FINE THEN LEAVE AS IS IF DOESN'T WORK THEN CONTINUE ON
01 | wait( 0.5 ) |
02 | CanAttack = true |
03 | local tool = script.Parent -- New Variable |
04 | tool.Touched:connect( function (part) -- change script.Parent with variable |
05 | local human = part.Parent:findFirstChild( "Humanoid" ) |
06 | if human then |
07 | if human.Parent.Name ~ = tool.Parent.Parent.Name and -- tool variable |
08 | CanAttack = = true and tool.Parent.Attack.Value = = true then -- tool variable |
09 | CanAttack = false |
10 | human:TakeDamage( 10 ) |
11 | wait( 0.5 ) |
12 | CanAttack = true |
13 | end |
14 | end |
15 | end ) |
IF WORKED PLEASE UP VOTE AND ACCEPT ANSWER! IF HAVE ANY QUESTION LEAVE COMMENT AND I WILL HELP
The problem is the ****player variable.
As the ROBLOX Wiki says, API:Class/Players/LocalPlayer only can be read-only, Attempting to write it will cause an error.
To fix this, you must use ****GetPlayerFromCharacter.
This code is a demonstration of GetPlayerFromCharacter
1 | tool = script.Parent |
2 | player = nil |
3 | character = nil |
4 |
5 | function onEquip() |
6 | character = tool.Parent |
7 | player = game.Players:GetPlayerFromCharacter(character) |
8 | end |
And therefore to fix this, you do.
01 | tool = script.Parent |
02 | player = nil |
03 | character = nil |
04 | CanAttack = true |
05 |
06 | function onEquip() |
07 | character = tool.Parent |
08 | player = game.Players:GetPlayerFromCharacter(character) |
09 | end |
10 |
11 | function onAttack() --script.Parent.Activated:connect(function() |
12 | local animation = character.Humanoid:LoadAnimation(script.Parent.AttackAnim) |
13 | if CanAttack = = true then --I don't use == true or false, I just if CanAttack then |
14 | CanAttack = false |
15 | human:TakeDamage( 10 ) |
16 | wait( 0.5 ) |
17 | CanAttack = true |
18 | end |
19 | end |
Please reply if errors included!
~I am a new player at scriptinghelpers