Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Animation for melee weapon gear only works in studio, any fix?

Asked by
ekosi 4
8 years ago

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.

01player = game.Players.LocalPlayer
02repeat wait() until player.Character
03character = player.Character
04CanAttack = true
05animation = character.Humanoid:LoadAnimation(script.Parent.AttackAnim)
06script.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
15end)

The second part is just for damage, and seems to work fine.

01wait(1)
02CanAttack = true
03script.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
14end)

Any help on this would be awesome. Thanks!

2 answers

Log in to vote
0
Answered by 8 years ago

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
02local player = game.Players.LocalPlayer -- added local
03repeat wait() until player.Character
04local character = player.Character -- added local
05local CanAttack = true -- added local
06local animation = character.Humanoid:LoadAnimation(script.Parent.AttackAnim) -- added local
07local tool = script.Parent -- a tool
08tool.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
19end)

there, all the green is what the changes are

NOTE: IF FINE THEN LEAVE AS IS IF DOESN'T WORK THEN CONTINUE ON

01wait(0.5)
02CanAttack = true
03local tool = script.Parent -- New Variable
04tool.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
15end)

IF WORKED PLEASE UP VOTE AND ACCEPT ANSWER! IF HAVE ANY QUESTION LEAVE COMMENT AND I WILL HELP

Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

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

1tool = script.Parent
2player = nil
3character = nil
4 
5function onEquip()
6       character = tool.Parent
7       player = game.Players:GetPlayerFromCharacter(character)
8end

And therefore to fix this, you do.

01tool = script.Parent
02player = nil
03character = nil
04CanAttack = true
05 
06function onEquip()
07       character = tool.Parent
08       player = game.Players:GetPlayerFromCharacter(character)
09end
10 
11function 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
19end

Please reply if errors included!

~I am a new player at scriptinghelpers

Answer this question