You can write your topic however you want, but you need to answer these questions:
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.
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?
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.
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?
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
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).
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.
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.
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.
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)]"