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

I'm terrible at lua. How to fix this morph script I made?

Asked by 7 years ago

Also thanks to the guy who answered my first question. I was an idiot and made it game.StarterGui instead of game.Players.LocalPlayer -_-

I only finished Roblox Lua beginner scripting and made a script for my game. Not really a morph script.

PlayerP = game.Players.LocalPlayer -- Will this get the name of the player??
function Click()
game.ServerStorage.THENAMEOFCHARACTER.Shirt:Clone() -- Clones the shirt and places it at the LocalPlayer's body??
game.ServerStorage.THENAMEOFCHARACTER.Shirt:Clone().Parent = workspace.PlayerP
end
script.Parent.MouseButton1Down:connect(Click)

I just spawn without it. Should I make this local script btw?

If I need to change anything then please tell me :c

0
It's not taking the shirt into the players body. And proably should be a Local Script. If you wan't me to help the comment. MineJulRBX 52 — 7y

1 answer

Log in to vote
1
Answered by
tkcmdr 341 Moderation Voter
7 years ago
Edited 7 years ago

Hey WHITEHH,

There are a number of problems with your script. They are:

1) Lack of indentation. Whenever you make a new code block of any kind (if, while, do, etc.) you should always increase indent within that block. Extra indentation aids in code readability. Having added some indentation, this is what your script looks like:

PlayerP = game.Players.LocalPlayer

function Click()
    game.ServerStorage.THENAMEOFCHARACTER.Shirt:Clone()
    game.ServerStorage.THENAMEOFCHARACTER.Shirt:Clone().Parent = workspace.PlayerP
end

script.Parent.MouseButton1Down:connect(Click)

2) Use of the connect function. This is deprecated, which means it is no longer recommended to use and may one day stop working. Use Connect instead.

3) Apparently there's something of the same name of the player in ServerStorage? Do yourself a favor and just store the shirt you want directly under ServerStorage.

4) Assigning the player object to PlayerP. game.Players.LocalPlayer is not a string, it's the actual player object. To get the name of the player, you'd want to write game.Players.LocalPlayer.Name. Additionally, if PlayerP were a string, game.Workspace.PlayerP would not work at all. To achieve the effect you want, use game.Players.LocalPlayer.Character.

5) Attempt to react to GUI events from a server script. Based on your saying "Should I make this local script btw?" I'm going to assume this is a server script. You should never use a server script to do client-side things (things that only happen in a particular player's game.) Local scripts are meant to do things on a player-by-player basis. If you're doing things like updating a player's GUI, there is no reason to use a server script.

6) Attempt to access ServerStorage from a local script. Having converted our script to a local script, we now have to change a player's shirt and have that change show for everyone else. This, by contrast to responding to GUI input, is something that the server should do. Therefore, we must make a separate server script to handle this. It should look something like this:

-- Server script in ServerScriptService

local AddShirtEvent = Instance.new("RemoteEvent")
local Shirt = game.ServerStorage:WaitForChild("Shirt")

-- When a RemoteEvent's OnClientEvent event is fired, the player is automatically passed.
local function OnAddShirtEvent(player)
    local newShirt = Shirt:Clone()

    -- We must parent the shirt to the player's character. To do that, we simply use player's Character property.
    newShirt.Parent = player.Character
end

AddShirtEvent.Name = "AddShirtEvent"
AddShirtEvent.Parent = game.ReplicatedStorage

AddShirtEvent.OnClientEvent:Connect(OnAddShirtEvent)

To tell the server to add the shirt, we simply change the local script to look like this:

local AddShirtEvent = game.ReplicatedStorage:WaitForChild("AddShirtEvent")

local function OnButtonClicked()
    AddShirtEvent:FireServer()
end

script.Parent.MouseButton1Click:Connect(OnButtonClicked)

With all that being said, you might want to check out NoahWillCode's Clean and Tidy, as well as this forum post by yours truly. You really ought to flesh out your knowledge of Lua, as this six line script your posted has more fundamental problems than a square wheel. Hate to say it, but that's just the truth. -_-

Have a nice day, WHITEHH, and best of luck learning Lua.

Truly yours, tkcmdr

Resources:

RBXScriptSignal Wiki Page

Player Wiki Page

FilteringEnabled and Client-Server relationship guide

Remote Event Wiki Page

WaitForChild Wiki Page

ServerStorage Wiki Page

ReplicatedStorage Wiki Page

Ad

Answer this question