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

Why wont this Armor script work?

Asked by 9 years ago

I have made this script, and it only works in studio. I dont seem to find the issue

Player = script.Parent.Parent
mouse = Player:GetMouse()
PlayersTorso = script.Parent.Parent.Character

function onKeyDown(key)
    key = key:lower()
    if key == "r" then
    local x = game.ServerStorage.KnightArmor["Knight Left Arm"]:Clone()
    local s = game.ServerStorage.KnightArmor["Knight Left Leg"]:Clone()
    local e = game.ServerStorage.KnightArmor["Knight Right Arm"]:Clone()
    local t = game.ServerStorage.KnightArmor["Knight Right Leg"]:Clone()
    local p = game.ServerStorage.KnightArmor["Knight torso"]:Clone()
    x.Parent = PlayersTorso
    s.Parent = PlayersTorso
    e.Parent = PlayersTorso
    t.Parent = PlayersTorso
    p.Parent = PlayersTorso


    end
end

mouse.KeyDown:connect(onKeyDown)

0
Is this a LocalScript? BlueTaslem 18071 — 9y
0
Ya, its in the startergui. UltraUnitMode 419 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

Well, the first problem I spotted is that you're not waiting for the Player's Character. The difference between In-game and Playsolo is that you have to wait for the player's character to load. It takes a while doesn't it?

So how would we do this, you ask? We'll repeat wait() until the character is found.

repeat wait() until game.Players.LocalPlayer.Character

Another thing I spotted is that you're defining Player as a bunch of Parents. There is a better alternative to this. LocalPlayer. LocalPlayer is basically something that can only be used in Local Scripts to define the Player which controls the client directly. We can get it through Players.

local Player = game.Players.LocalPlayer
local PlayersTorso = Player.Character

And the final fix, let me introduce you to something called UserInputService. We could also use ContextActionService, but UserInputService is a bit less advanced and understandable. Since Keydown is depricated (and not to mention it lags every time someone presses something that uses Keydown)

UserInputService on the wiki if you want to see it here

So with UserInputService in mind, let's write it using UserInputService instead of Keydown.

The first thing we have to do is get the Service UserInputService.

local Input = game:GetService("UserInputService")

Now we have to set up a script that does something every time the button is pressed. We could do this by connecting the event InputBegan

local Input = game:GetService("UserInputService")
Input.InputBegan(Enum.KeyCode.r):connect(function()
    local x = game.ServerStorage.KnightArmor["Knight Left Arm"]:Clone()
    local s = game.ServerStorage.KnightArmor["Knight Left Leg"]:Clone()
    local e = game.ServerStorage.KnightArmor["Knight Right Arm"]:Clone()
    local t = game.ServerStorage.KnightArmor["Knight Right Leg"]:Clone()
    local p = game.ServerStorage.KnightArmor["Knight torso"]:Clone()
    x.Parent = PlayersTorso
    s.Parent = PlayersTorso
    e.Parent = PlayersTorso
    t.Parent = PlayersTorso
    p.Parent = PlayersTorso

end)

So with all of this, the final script.

repeat wait() until game.Players.LocalPlayer.Character -- repeat waiting for the character to load
Player = game.Players.LocalPlayer -- define the player as localplayer
mouse = Player:GetMouse()
PlayersTorso = Player.Character
Input = game:GetService("UserInputService")

Input.InputBegan(Enum.KeyCode.r):connect(function()
    local x          = game.ServerStorage.KnightArmor["Knight Left Arm"]:Clone()
    local s      = game.ServerStorage.KnightArmor["Knight Left Leg"]:Clone()
    local e          = game.ServerStorage.KnightArmor["Knight Right Arm"]:Clone()
    local t          = game.ServerStorage.KnightArmor["Knight Right Leg"]:Clone()
    local p          = game.ServerStorage.KnightArmor["Knight torso"]:Clone()
    x.Parent         = PlayersTorso
    s.Parent         = PlayersTorso
    e.Parent         = PlayersTorso
    t.Parent         = PlayersTorso
    p.Parent         = PlayersTorso
end


Ad

Answer this question