MaxHealth and walk speed script is not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to have my script set the players MaxHealth and Walkspeed on spawn AND whenever the variable changes.

  2. What is the issue?

local player = game.Players.LocalPlayer
local char = player.Character
local speed = player.Stats.Speed
local max = player.Stats.Health


speed.Value.Changed:connect(function()
	char.Humanoid.Speed = speed.Value
end)

max.Value.Changed:connect(function()
	char.Humanoid.MaxHealth = max.Value
end)

function onPlayerRespawned()
	wait(2) --loading delay
		char.WalkSpeed = speed.Value
		char.MaxHealth = max.Value
	end

game.Workspace.ChildAdded:connect(onPlayerRespawned)

It doesn’t work, there is not even an error?

  1. What solutions have you tried so far?
    Well I have been trying with this script for two days but due to it’s short size and the fact that it is throwing no errors It is kind of hard to troubleshoot. The script is a LocalScript by the way.
1 Like

Hello, please do not use game.Workspace.ChildAdded as the player instance has an event called CharacterAdded. Also, are you sure that Stats is a child of the player and speed/max is a child of Stats?

1 Like

the player stats are whatever they are by default so I don’t know. I am sure that the others are children of Stats though.

Can you try printing speed.Value and max.Value?

game.Workspace.CharacterAdded:connect(onPlayerRespawned)

Like this? as for the prints I tested both sections

print {char.Humanoid.MaxHealth.Value}
print (speed.Value)

they output nothing.

Can you show me your server script making the Stats folder and Speed/Health values?

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("UserData11")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Data = {
	Strength = 0;
	Rebirths = 0;
	Cash = 0;
	Speed = 116;
	Damage = 5;
	Health = 100;
	Weight = 1;
	Alien = false;
	Bear = false;
	Bee = false;
	Bunny = false;
	Burger = false;
	Cat = false;
	Cow = false;
	Dog = false;
	Donut = false;
	Forgotton = false;
	Fox = false;
	Jellyfish = false;
	Mouse = false;
	Demon = false;
	Spook = false;
	TV = false
	}



local playersavetable = {};

local function loadStarterData(Player)
		local Stats = Instance.new("Folder")
		Stats.Name = "Stats"
		Stats.Parent = Player
		local Inventory = Instance.new("Folder")
		Inventory.Name = "Inventory"
		Inventory.Parent = Stats
		for statname, statvalue in pairs(Data) do
			if type(statvalue) == 'number' then
			local intvalue = Instance.new("NumberValue")
			intvalue.Name = statname
			intvalue.Value = statvalue
			intvalue.Parent = Stats
			else if type(statvalue) == 'boolean' then
			local intvalue = Instance.new("BoolValue")
			intvalue.Name = statname
			intvalue.Value = statvalue
			intvalue.Parent = Inventory
		end
	end
		end
		end

local function loadData(player, Stats)
	local Data = {}
	local Stats = Stats or player.Stats
	local s, e = pcall(function()
	Data = DataStore:GetAsync('UserId'..player.UserId)
	end)
	
	if s then 
		print (player.Name.."Data loaded")
	else
		print(player.Name.."Data failed to load")
	end
	
	if Data then
		for statname, statvalue in pairs(Data) do
			if type(statvalue) == "number" then
			player.Stats[statname].Value = statvalue
			else if type(statvalue) == "boolean" then
			player.Stats.Inventory[statname].Value = statvalue
			end
			end
		end
		print(player.Name.."Data has been loaded")
	else
		print(player.Name.."No data found! generating..")
		end
	end

local function saveData(player)
	--if RunService:IsStudio() then return end
	local Data = {}
	for _, stat in ipairs(player.Stats:GetChildren()) do
   if not stat:IsA("Folder") then
       Data[stat.Name] = stat.Value
   end
end
	local s, e = pcall(function()
		DataStore:SetAsync('UserId'..player.UserId, Data)
	end)
		if s then 
	print(player.Name.."Data has been saved")
		else
	warn (player.Name.."Data failed to save"..e)
	end
end

ReplicatedStorage.SaveEvent.OnServerEvent:Connect(function(player)
 saveData(player)	
end)

Players.PlayerAdded:Connect(function(player)
	playersavetable[player] = tick()
	loadStarterData(player)
	loadData(player)
   
   local Stats = player:WaitForChild("Stats")
   
  local RemoveEvent
   RemoveEvent = Players.PlayerRemoving:Connect(function(vplayer)
      if vplayer == player then
         saveData(vplayer, Stats)
         RemoveEvent:Disconnect()
      end
   end)
end)

In other LocalScripts I can put the value on to GUIs with this

Damage = game.Players.LocalPlayer.Stats.Damage

script.Parent.Text = Damage.Value
Damage.Changed:connect(function()
	script.Parent.Text = Damage.Value
end)

Try replacing your local script to a server script inside StarterCharacterScripts as it will create the script once the character has spawned that way you don’t need to add a delay.

And to get the character you just need to make a variable to the parent ; local char = script.Parent
and if you want to get the player ; local Player = game.Players[char.Name] (this isn’t the best way but it works).

Hopefully this helps.

1 Like

Thanks, but now we are getting an output. " [ Workspace.TyDye_RBLX.Script:7: attempt to index field ‘Value’ (a number value)]"

Yes. Note that you can change health on the client, but it will not affect the server, in fact the server will override it if there are health scripts. As for the WalkSpeed, often times these kinds of childadded functions are connected but don’t initially run. I suggest making a separate function that is called right after you connect it.

Isn’t that just this


function onPlayerRespawned()
game.Workspace.CharacterAdded:connect(onPlayerRespawned)
	wait(2) --loading delay
		char.WalkSpeed = speed.Value
		char.MaxHealth = max.Value
	end

The code should be in the function and the event can be connected to that function like this:

function onPlayerRespawned()
	wait(2) --loading delay
	char.WalkSpeed = speed.Value
	char.MaxHealth = max.Value
end
game.Players[playerName].CharacterAdded:connect(onPlayerRespawned)

You can’t do game.Workspace.CharacterAdded:Connect(). Instead, do player.CharacterAdded:Connect()

sorry I just copied the code that was there and reformatted it, fixed now

local char = script.Parent
local player = game.Players[char.Name]
local speed = player.Stats.Speed
local max = player.Stats.Health


speed.Value.Changed:connect(function()
	char.Humanoid.Speed = speed.Value
end)

max.Value.Changed:connect(function()
	char.Humanoid.MaxHealth = max.Value
end)


print {char.Humanoid.MaxHealth.Value}
print (speed.Value)

function onPlayerRespawned()
	wait(2) --loading delay
		char.WalkSpeed = speed.Value
		char.MaxHealth = max.Value
end
player.CharacterAdded:Connect()

This is the script I have now, outputs “Workspace.TyDye_RBLX.Script:7: attempt to index field ‘Value’ (a number value”

He can just do player.CharacterAdded:Connect() since he already has a player variable defined in the original post. By the way, use Connect with a capital C because the lowercase c version is deprecated.

By the way, you have to pass the onPlayerRespawned function in the parentheses after :Connect.

1 Like

I should have payed more attention to the code, my bad.
@TyDye_RBLX You just have to remove the “Value” in “Value.Changed” because the Changed event fires when the value is changed. It’s an event of the value itself, while “Value” is a property of the value.

He could use GetPropertyChangedSignal which focuses on a specific propery.

Well that fixes the error, dosen’t seem to change the users speed though. I also removed the .Value from the second one but strangely it outputs this " [ Workspace.TyDye_RBLX.Script:16: attempt to index field ‘MaxHealth’ (a number value)]"