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

player = game.Players.LocalPlayer
repeat wait() until player.Character
character = player.Character
CanAttack = true
animation = character.Humanoid:LoadAnimation(script.Parent.AttackAnim)
script.Parent.Activated:connect(function()
    if CanAttack == true then
        CanAttack = false
        script.Parent.Attack.Value = true
        animation:Play()
        wait(0.5)
        CanAttack = true
        script.Parent.Attack.Value = false
    end
end)

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

wait(1)
CanAttack = true
script.Parent.Touched:connect(function(part)
    local human = part.Parent:findFirstChild("Humanoid")
    if human then
        if human.Parent.Name ~= script.Parent.Parent.Parent.Name and
            CanAttack == true and script.Parent.Parent.Attack.Value == true then
            CanAttack = false
            human:TakeDamage(10)
            wait(0.5)
            CanAttack = true
        end
    end
end)

Any help on this would be awesome. Thanks!

2 answers

Log in to vote
0
Answered by 7 years ago

Okie time to help,

I will tell you all the problems

First part (Script 1)

-- in local scripts when using variables always use local and WaitForChild allows the system find the variable quicker 
local player = game.Players.LocalPlayer -- added local
repeat wait() until player.Character
local character = player.Character -- added local
local CanAttack = true -- added local
local animation = character.Humanoid:LoadAnimation(script.Parent.AttackAnim) -- added local
local tool = script.Parent -- a tool 
tool.Activated:connect(function()  --with the new variable we change script.Parent with tool
    if CanAttack == true then
    wait(0.2) -- let the system calm down
        CanAttack = false
        tool:WaitForChild("Attack").Value = true -- use tool
    wait(0.1) -- let the system cool
        animation:Play()
        wait(0.5)
        CanAttack = true
        tool:WaitForChild("Attack").Value = false -- use the variable tool and add WaitForChild
    end
end)

there, all the green is what the changes are

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

wait(0.5)
CanAttack = true
local tool = script.Parent -- New Variable
tool.Touched:connect(function(part) -- change script.Parent with variable
    local human = part.Parent:findFirstChild("Humanoid")
    if human then
        if human.Parent.Name ~= tool.Parent.Parent.Name and -- tool variable
            CanAttack == true and tool.Parent.Attack.Value == true then -- tool variable
            CanAttack = false
            human:TakeDamage(10)
            wait(0.5)
            CanAttack = true
        end
    end
end)

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 7 years ago
Edited 7 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

tool = script.Parent
player = nil
character = nil

function onEquip()
       character = tool.Parent
       player = game.Players:GetPlayerFromCharacter(character)
end

And therefore to fix this, you do.

tool = script.Parent
player = nil
character = nil
CanAttack = true

function onEquip()
       character = tool.Parent
       player = game.Players:GetPlayerFromCharacter(character)
end

function onAttack() --script.Parent.Activated:connect(function()
       local animation = character.Humanoid:LoadAnimation(script.Parent.AttackAnim)
       if CanAttack == true then --I don't use == true or false, I just if CanAttack then
              CanAttack = false
              human:TakeDamage(10)
              wait(0.5)
              CanAttack = true
       end
end

Please reply if errors included!

~I am a new player at scriptinghelpers

Answer this question