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

Why does the sword disappear a second after being "Summoned"?

Asked by 7 years ago

Thank you, in advance, for the help :) So I created a "swordSummoning" (local) script located in StarterPlayerScripts. It's meant to equip the user with a sword once the 'r' key is pressed. Each time the function is called, the sword appears for about a second or two, then completed disappears. On top of that, the sword's "handle" does not appear.

Here is the code within the Summoning script:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
hasSword= false
Mouse.KeyDown:connect(function(Key)
    if Key == "r" and hasSword == false then                --If the key is pressed and player does not have a sword
        hasSword= true                              --Make the hasSword variable true
        clone = game.Lighting.Sword:Clone()             --Create a clone of the sword
        clone.Parent = game.Players.LocalPlayer.Character --Put the sword on the character
        print("Worked")                             --Print "Worked" for assurance
    else                                                --If you do have the sword
        clone:Destroy()                             --Remove it
        hasSword= false                             --Make it false
    end 
end)

I tried to get rid of this...:

else                                                --If you do have the sword
        clone:Destroy()                             --Remove it
        hasSword= false                             --Make it false

...but nothing changed. I still got the same problem. So I'm starting to think that it doesn't have anything to do with the Summoning script

Question: Is the script the problem? If yes, how can I fix the script?

1
You should never use Mouse.Keydown! It's deprecated! Validark 1580 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Since the beginning of the if statement has

If Key == "r"

that part of the if statement and only the function below it will work if they key is R

On the other hand the

else

at line 10 doesn't have "If Key == "r"" Therefore that function can be triggered by any button.

Sorry if I'm not good with explanations, but that's why it's disappearing. Just change the else to

elseif Key == "r" then

then you'll be good.

0
That didn't work secretboy6 68 — 7y
0
But thank you for the help! secretboy6 68 — 7y
0
Downvoted for encouraging use of Mouse.Keydown Validark 1580 — 7y
0
It's deprecated, that's why. Here's a wiki page: http://wiki.roblox.com/index.php?title=Keyboard_input MapleGalaxy 8 — 7y
Ad

Answer this question