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

Script not changing Health?

local Players = Game:GetService("Players")
print("STARTING")
Players.PlayerAdded:connect(function(plr) 
    local health = plr:WaitForChild("leaderstats"):WaitForChild("Health") 
    plr.CharacterAdded:connect(function(char) 
	if health.Value < 100 then
		health.Value = 100
	end
        char:WaitForChild("Humanoid").MaxHealth = health.Value 
		print("Provided health on join")
    end)
    health.Changed:connect(function(h) 
        if plr.Character then 
            plr.Character:FindFirstChild("Humanoid").MaxHealth = h 
			print("Provided health on respawn")
        end
    end)
end)

This is currently placed within the ServerScriptStorage. It's currently not doing anything. Is there anything wrong with it? (The output doesn't say anything, not even the prints.)

function DataHandler(p,o,key)	
	local types = {
		IntValue = "Number";
		NumberValue = "Number";
		ObjectValue = "Instance";
		BoolValue = "Boolean";
		StringValue="String";
	}
	local t = types[o.ClassName]
	if (not t) then return end
	key = (type(key) == "string" and key or o.Name)	
	key = (#key > 0 and key or key.Name == o.Name and o.ClassName or o.Name)
	if (not p.DataReady) then												
		p:WaitForDataReady()					
	end											
	local Save,Load = p[("Save%s"):format(t)],p[("Load%s"):format(t)]	
	o.Value = Load(p,key)	
	local lastV = o.Value
	o.Changed:connect(function(v)
		lastV = v
		Delay(1,function()		
			if (lastV ~= v) then return end
			Save(p,key,v)
		end)
	end)
end


game.Players.PlayerAdded:connect(function(player)
	local l = Instance.new("IntValue",player)
	l.Name = "leaderstats"
	
	local kill = Instance.new("IntValue",l)
	kill.Name = "KOs"
	DataHandler(player,kill,"Thekills")
	
	local death = Instance.new("IntValue",l)
	death.Name = "Wipeouts"
	DataHandler(player,death,"Thedeaths")
	
	local mh = Instance.new("IntValue",l)
	mh.Name = "Health"
	DataHandler(player,mh,"MaxHealths")
end)

2 answers

This looks like something I answered earlier, did you post this again?

Anyway, plr.CharacterAdded will load too late, if it's waiting for 'health'.

Why don't you contain this with your leaderboard script, or whatever loads your leaderstats?...

e.g.

--Server-type script in ServerScriptService--

game.Players.PlayerAdded:connect(function(plr)
	Instance.new("IntValue",plr).Name = "leaderstats"
	local health = Instance.new("NumberValue",plr.leaderstats)
	health.Name = "Health"

	if health.Value < 100 then health.Value = 100 end


	plr.CharacterAdded:connect(function(char)
		char:WaitForChild("Humanoid").MaxHealth = health.Value
	end)


	health.Changed:connect(function()
		char:WaitForChild("Humanoid").MaxHealth = health.Value
	end)
end)

3 comments

0

The thing is, I have a script that provides the IntValue's already. And it runs off of datasave. I want a different script to edit the IntValue's.

0

Seems a little in-efficient, and the timing may get messed up. But if you can figure a way to do it.

0

Do you just want me to upload the datasave script so you can edit it?

ServerScriptStorage is for storing scripts. Scripts inside there will not run. ServerScriptStorage is for keeping scripts safe, for example if a game breaks, the creator has a script that extracts the script from SSS(Let's call ServerScriptStorage that for now) to fix the game. --EDIT-- I guess it isn't for that? When I put scripts in there, it never runs...

2 comments

0

It's ServerScriptService, it's used to hold Server Scripts.

0

oh, sowwy :c

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