Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script to make player die upon joining, and only once?

Recently I switched from Data Persistence to Data Storage, and my level up script will not work until the player has reset after the first time joining, and I get errors saying "this is not a valid member of that" Until the player has reset. So I thought about instead of adding WaitForChilds in all of my scripts, I would just add a simple script that makes the player die and be forced to re-spawn when they join the game, to ensure that everything works right. Here is my attempt, which I could not get to work:

game:GetService('Players').PlayerAdded:connect(function(player)
    player.Character.Health = 0
    end

My attempt is so bad that it almost seems lazy to me, but then again, I've never been faced with this situation.

Any help is greatly appreciated, thank you.

Edit: Here is that Data Storage script and the Level up script

Data Storage:

local DSService = game:GetService('DataStoreService'):GetDataStore('Stats')
ID = 68 
SaveTime = 60
leaderboardname = "Levelss" --The name of the leader board

StatList = {Muny = 250 , Level = 1 , XP = 0 , FAP = 1 , RandomMove = 0 , RangedPunch = 0 }



function CreateValue(Name, Parent, Value) --If value is set to a number then new players will start with that amount.
    Value = Value or 0
    local savevalue =  Instance.new('IntValue', Parent)
    savevalue.Name = Name
    savevalue.Value = Value
    return savevalue
end

function GetValue(uniquekey)
    -- GetAsync
    local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
        return GetSaved
    end
end

game.Players.PlayerAdded:connect(function(plr)
    local uniquekey = ID..plr.userId
    local Levelss = Instance.new('IntValue', plr)
    Levelss.Name = leaderboardname
    for i,v in pairs(StatList) do
        local Stat = GetValue(i..uniquekey)
        if Stat then
            CreateValue(i, Levelss, Stat) --Old player load their value
        else
            CreateValue(i, Levelss, v) --New player load the default value
        end
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local uniquekey = ID..plr.userId
    if plr:FindFirstChild(leaderboardname) then
        for i,v in pairs(StatList) do
            if plr[leaderboardname]:FindFirstChild(i) then
                DSService:SetAsync(i..uniquekey, plr[leaderboardname][i].Value)
            end
        end
    end
end)

while wait(SaveTime) do
    P = game:GetService("Players"):GetChildren()
    for o=1, #P do
        if P[o] then
            local uniquekey = ID..P[o].userId
            if P[o]:FindFirstChild(leaderboardname) then
                for i,v in pairs(StatList) do
                    if P[o][leaderboardname]:FindFirstChild(i) then
                        DSService:SetAsync(i..uniquekey, P[o][leaderboardname][i].Value)
                    end
                end
            end
        end
    end
end



Level up: (isnt 100% finished but it does still modify level so it's good enough for testing)

s = script.Parent
p = s.Parent
c = p.Character



p.Levelss.XP.Changed:connect(function()
if p.Levelss.XP.Value >= p.Levelss.Level.Value * 20 then
p.Levelss.XP.Value = 0
p.Levelss.Level.Value = p.Levelss.Level.Value + 1
p.Character.Humanoid.MaxHealth = p.Character.Humanoid.MaxHealth + 10
p.Character.Humanoid.Health = p.Character.Humanoid.Health + 10
p.Levelss.FAP.Value = p.Levelss.FAP.Value + 1
end
end)

6 comments

0

Why do you need to reset the player? can you include the level up script.

0

Basically the Data does not load before the character is added, which is why it needs to be reset. But, nothing will work until the character is reset. Scripts cant find values, for example, Player.Data.Level would come up as (Level is an invalid member blah blah) Resetting fixes all of that, and I'd rather just reset the player for now, and bother with fixing the whole thing later on down the roa

0

A data store does not need the player to save / load data

0

Can you include the full script.

0

There is the data storage and level up script

0

why not use :BreakJoints() on the Character?

3 answers

✓ Accepted answer

Well, there's a few key points you seem to miss. This code will not run because: 1. You are asking it to modify the health of Character which is not Humanoid --2. You are resetting their health to 0 when the player joins, not the character getting added. If you want it to only happen once, then put a repeat wait in the end until the character gets spawned. Example:

game:GetService('Players').PlayerAdded:connect(function(player) --Player joins
	--Run all Data Storage scripts
	repeat --Wait until the character becomes a thing
		wait()
	until player.Character ~= nil
	player.Character.Humanoid.Health = 0 -- Now, reset Humanoid's health
end

This is not the best way to go about solving the issue of the data not loading until they reset, but that's the help I can offer with your question :P -- Have a good day, and I hope I helped!

5 comments

0

Thanks for your help but, that doesn't seem to fix the problem. The player is not getting their hp reset when they join, and I do not see any errors that pertain to the script

0

^ Where is it going wrong? Just put prints everywhere and tell us where

0

wow.. Sorry, iamnoamesa, your code did work, I just forgot to put a ')' after the end. It works now, thank you

0

No problem

0

u could use :BreakJoints instead of setting health to 0

Player.Character.Health = 0 This does like you set the character model's health to 0. Use Player.Character:FindFirstChild("Humanoid").Health = 0

2 comments

0

Or he can use the BreakJoints function.

0

may work too, tho i never used it so idk.

There is a problem with roblox at the moment chatted is broken aswell.

This discussion is preserved as it was. New answers, comments, and votes are closed.