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

Local Script Problems, any help?

Asked by 9 years ago

I need to change the MaxHealth of a every player 30. I put a local script in the StarterPack with this code in it.

local name = script.Parent.Parent.Parent.Name
local person = game.Workspace:findFirstChild(name)
wait(5)
 person.Humanoid.MaxHealth = 30

It's fairly simple, but it doesn't work I have no clue why, any help? What do I need to change?

3 answers

Log in to vote
1
Answered by 9 years ago

First off, you have too many "parents" in your name variable. Really, you shouldn't be accessing the player this way anyways, because it wouldn't work. It is very inefficient, here is a much better way to do it.

LocalScripts have a built in variable that only LocalScripts can use. It is game.Players.LocalPlayer.

So, instead of finding the Player's name to find the Player, you can use LocalPlayer.

local player = game.Players.LocalPlayer
    if player.Character ~= nil then
        player.Character.MaxHealth = 30
    end
end

That will wait for the player's character to load, then change it's MaxHealth to 30. You don't even have to access the Player in the Workspace, only in the Players.

Ad
Log in to vote
0
Answered by 9 years ago

Figured it out, but thanks.

If anyone needs it, here:

function Added(Model)
wait(5);
if Model == nil then return end
if Model.Name == "Humanoid" then
if Model == nil then return end
print("MaxHealth = 30");
Model.MaxHealth = 30;
end
end
game.Workspace.DescendantAdded:connect(Added)
Log in to vote
-3
Answered by 9 years ago

first of all u need to change ur words around a little: change person to plr

second of all u may be using the wrong type of script and if this doesnt work then it will probably be where u placed ur script... in other words ur script probably needs to be in the workspace instead of starterpack.

tip: starterpack is mostly used for items and objects u want the player to spawn with and usually does not hold scripts alone.

so ur new script should look somehting like this:

local name = script.Parent.Parent.Parent.Name

local plr = game.Workspace:findFirstChild(name)

wait(5)

plr.Humanoid.MaxHealth = 30

BUT I AM JUST A STARTING SCRIPTER SO THERE IS ALSO A LARGE CHANCE THIS IS NOT RIGHT!!!!!!!!!!!!

0
That is not at all right. You can name a variable whatever you would like, as long as it includes supported characters... SlickPwner 534 — 9y
0
sorry i tried FastSnail5 8 — 9y
0
StarterPack is great for scripts once you're a bit more advanced. It allows you to have mouse-based tools that don't need to be run through a tool, making it easier (and more interesting) to the player. RoboFrog 400 — 9y
0
o... i did not know that FastSnail5 8 — 9y

Answer this question