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

When I go from one place to another, my health goes back to normal?

Asked by 9 years ago

I have a GUI that teleports you to an obby. When you finish the obby, you can choose to add 5 to your health, but when you go back to the main game, you lose all that health! Why?

function onTouched(part)
if script.Parent.Available.Value == 0 then
local h = part.Parent:findFirstChild("Humanoid")
if h~=nil then
--my part
h.MaxHealth = h.MaxHealth + 5
h.Health = h.Health + 5
wait(0.001)
script.Parent.Available.Value = 1
    local human = part.Parent:findFirstChild("Humanoid")
    if (human ~= nil ) then 
        part.Parent.Torso.CFrame = CFrame.new(Vector3.new(-0.6, 27.5, -3))

    end
wait(1)
script.Parent.Available.Value = 0
--end of my part
end
end
end
script.Parent.Touched:connect(onTouched)

~this is not my entire script, i just did the middle part Please help! Thank you!

3 answers

Log in to vote
1
Answered by 9 years ago

I think you need to either give more code or be a bit more specific about what is happening. There are a few things I'd like to point out, though...

if (human ~= nil ) then

You can do a more simple check than this, such as the following...

if (not human) then

This does the exact same thing, it's just easier to type and a bit more conventional. However, this is just my nitpicking, so if you dislike that style, then feel free to disregard it.

Another thing,

part.Parent:findFirstChild("Humanoid")

That should definitely cause an error. It seems that you know another language where the first letter of a function is not capitalized. This is not the same in the case of RBX.Lua.

part.Parent:FindFirstChild("Humanoid")

That is the correct syntax.

This is another nitpick thing, so bear with me. When you create if statements, if they're really simple and just check values, try to just make them one-liners so that you don't end up with harder-to-read code.

For example, this:

if script.Parent.Available.Value == 0 then
    ...
end

I would go with the cleaner,

if script.parent.Available.Value ~= 0 then return end

That said, this should only be used in certain cases. Because this returns, it will stop whatever code block that it's executing, so this should mostly only be used in a function, like you have here and a few other cases.

I hope this helps a bit. - Programmix

0
Not what I was asking, the script works fine, but the health doesn't transfer when teleporting through games yogipanda123 120 — 9y
0
Well, that's totally to be expected. If you want the health to transfer over, you'd probably be best-off using DataStores to transfer that data. Programmix 285 — 9y
Ad
Log in to vote
0
Answered by
parkderp1 105
9 years ago

What's the output?

Log in to vote
0
Answered by
yumtaste 476 Moderation Voter
9 years ago

Since you're joining a new place, of course your health will be reset. There is a way around this, and that way is by using DataStores.

Before I begin:

I assume when you said "[you] have a GUI that teleports you to a lobby," you mean you are utilizing the TeleportService to move players inbetween Places that are under one Universe (I can explain that whole thing if you need me to).

Here's the answer:

DataStores are useful for tons of things. In your case, you need to use them for transferring health. I'll actually write the code and explain it as I go.

local healthStore = game:GetService("DataStoreService"):GetDataStore("healthStore") --this will create a DataStore called "healthStore" if it has not already been created. I recommend you test with other names, such as "testHealthStore" and give it different numbers at the end, so you don't have to test on the real store. That isn't important for this since you're just using DataStores to remember a player's health, but it will be useful for things such as leaderboards and keeping track of stats.

game.Players.PlayerRemoving:connect(function(player)
player:UpdateAsync(player.Name.." saved health", player.Character.Humanoid.Health)
end)

Now that you have the unnecessarily long DataStore explanation and the save code, here's how you would access it:

local healthStore = game:GetService("DataStoreService"):GetDataStore("healthStore") --MUST BE THE SAME AS THE SAVE SCRIPT!

game.Players.PlayerAdded:connect(function(player)
player.Character.Humanoid.Health = healthStore:GetAsync(player.Name.." saved health") --sets the Humanoid's health to the saved number, which is the health when the player left the previous Place
end)

Note that if these are 2 separate game places that are not part of the same Universe, it's not possible to save/load data. For example, if someone had BC and had 2 places open, they could not communicate with each other. If you have any questions, please feel free to comment!

If this answer helps, please **upvote and accept*!

0
Thank you so much! I didn't know calling the service created a storage! I though it had to access a value of some sort! THANK YOU SO MUCH!!! yogipanda123 120 — 9y
0
wait a second yogipanda123 120 — 9y
0
Do you have to put a datastore and "recieve" in both places? yogipanda123 120 — 9y
0
The script(s) are just in workspace right? Not in any specific place? yogipanda123 120 — 9y
View all comments (2 more)
0
I tried doing it myself, didn't work, copied yours, didnt work! WHY! yogipanda123 120 — 9y
0
Both scripts are in the obby place. if it doesn't work, tell me. yumtaste 476 — 9y

Answer this question