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

Point system breaks when you die. Help?

Hello! My point system (gives you points when you walk) stops working when you die. Does anyone know how to fix this?


--SAVE SYSTEM--

local DSService = game:GetService('DataStoreService'):GetDataStore('Hamburger223232')
game.Players.PlayerAdded:connect(function(plr)
	-- Define variables
	local uniquekey = 'id-'..plr.userId
	local leaderstats = Instance.new('IntValue', plr)
	local savevalue =  Instance.new('IntValue')
	leaderstats.Name = 'leaderstats'
	savevalue.Parent = leaderstats
	savevalue.Name = 'Steps'
	
	-- GetAsync
	local GetSaved = DSService:GetAsync(uniquekey)
	if GetSaved then
		savevalue.Value = GetSaved[1]
	else
		local NumbersForSaving = {savevalue.Value}
		DSService:SetAsync(uniquekey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local Savetable = {plr.leaderstats.Steps.Value}
DSService:SetAsync(uniquekey, Savetable)	
	
	
end)

--POINT ADDER--

local amount = 1;
local points = "Steps";
local wte = wait;
local vec3 = Vector3;

local function CheckForWalk(plr, hum)
    pcall(function()
        while wte() do
            if hum.MoveDirection ~= vec3.new(0, 0, 0) then --if the character is moving
                while wte(1/plr.leaderstats["Steps"].Value) do --while the player is moving, increase their step points each second.
                    if hum.MoveDirection ~= vec3.new(0, 0, 0) then
                        plr.leaderstats[points].Value = plr.leaderstats[points].Value + amount;
                    else
                        break;
                    end
                end
            end
        end
    end);
end

game.Players.PlayerAdded:connect(function(plr)
    repeat
        wte();
    until plr.Character;
    local hum = plr.Character:WaitForChild("Humanoid");
    CheckForWalk(plr, hum);
    plr.Changed:connect(function(prop)
        repeat wte() until plr.Character;
        local hum = plr.Character:WaitForChild("Humanoid");
        CheckForWalk(plr, hum);
    end);
end);

1 answer

You need to use a CharacterAdded event instead of the Changed event on the player returned from the PlayerAdded event.

You can define your hum variable from the parameter returned from the CharacterAdded event, rather than waiting for the Character and indexing it..

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid");
		CheckForWalk(plr, hum);
	end)
end)

8 comments

0

It seems to have the same problem :(

0

Actually, it fixed one of the problems. The only issue is if you hold in "W" it breaks and gives you points when you aren't walking. Sorry if I'm asking too much, but do you know why that happens?

0

I would just need to make it so when the player dies, it stops the function from running for 10 seconds.

0

Do you know how I could do that?

0

Already told you :)

1

I understand what you mean now. Sorry, I didn't really know how check if a player died. I think I got it, and I can add wait(5) under it as a debounce. Thanks for helping me out.

0

No problem :D I was hoping you'd either figure it out or ask another question about debounce. Happy developing.

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