A script problem

This is my first post. Tell me if something is wrong! :smiley:

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

  1. What do you want to achieve? Keep it simple and clear!
    I want to make this script work for R6, also there is a bug, it resets ā€œRaceā€ if player resets

  2. What is the issue? Include screenshots / videos if possible!
    Player changes back to their normal size and Gui stops displaying ā€œRaceā€.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried activating ā€œChangeBody()ā€ and ā€œChange()ā€ after player resets. (i am new to scripting)
    this is an example of how i tried to fix it:

Humanoid.Died:Connect(function()
ChangeBody()
Change()

It didn’t work since those functions reset after player respawns.
The code that i need to change (it has some notes):

local DataSaving = game:GetService("DataStoreService"):GetDataStore("Saving001")

game.Players.PlayerAdded:Connect(function(Player) 
	local Character = Player.Character or Player.CharacterAdded:Wait() 
	local Humanoid = Character:WaitForChild("Humanoid") 
	local PlayerGui = Player:WaitForChild("PlayerGui") 
	
	
	local Races = {
		Human = 60, 
		Demon = 5,
		Elf = 20,
		Giant = 40;
	}

	local sum = 0
	for _,Values in pairs(Races) do
		sum = sum + Values
	end

	local function getRandomItem()
		local RaceValue = Random.new():NextNumber(0, sum)
		for item, value in pairs(Races) do
			RaceValue = RaceValue - value
			if RaceValue < 0 then
				return item
			end
		end
	end
			-- the function below changes the size of the players body (but in r15)

	local function ChangeBody()
		local RaceNames = Player.DataFolder.Race.Value

		local Parts = Character:GetChildren()
		
		
		local BodySizeParts = {
			Humanoid.HeadScale,
			Humanoid.BodyDepthScale,
			Humanoid.BodyWidthScale,
			Humanoid.BodyHeightScale,
		}
		

		if RaceNames == "Giant" then
			for _,v in pairs(BodySizeParts) do
				v.Value = 2    	 -- scale
			end
		elseif RaceNames == "Elf" then
			for _,v in pairs(BodySizeParts) do
				v.Value = 0.6
			end
		elseif RaceNames == "Demon" then
			for _,v in ipairs(Parts) do
				if v:IsA("BasePart") then
					v.BrickColor = BrickColor.new("Really black")
				end
			end
		end

	end

	local DataFolder = Instance.new('Folder',Player)
	DataFolder.Name = "DataFolder"

	local Race = Instance.new("StringValue", DataFolder)
	Race.Name = "Race"

	local function Change() 		--changes text in StarterGui > RaceDisplay > TextButton > TextButton (i think, not quite sure, line 75)
		PlayerGui.RaceDisplay.TextButton.TextButton.Text = "Race: "..Race.Value
	end

	local Success, Value = pcall(function()
		return DataSaving:GetAsync(Player.UserId)
	end)

	if Success then
		if Value then
			Race.Value = Value[1]
		else
			Race.Value = tostring(getRandomItem())
		end
	else
		Race.Value = tostring(getRandomItem())
	end
	
	ChangeBody() 	 -- activates functions
	Change()
		

	end)
	


game.Players.PlayerRemoving:Connect(function(Player) 
	local Success, Err = pcall(function()
		DataSaving:SetAsync(Player.UserId, {Player.DataFolder:WaitForChild('Race').Value})
	end)
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		local Success, Err = pcall(function()
			DataSaving:SetAsync(player.UserId, {player.DataFolder:WaitForChild('Race').Value})
		end)
	end
end)

Everything would be very helpful! (I scripted it using a tutorial since i am new)

Try putting it all in this:


game.Players.PlayerAdded:Connect(function(Player) 
	local Humanoid = Character:WaitForChild("Humanoid") 
	local PlayerGui = Player:WaitForChild("PlayerGui") 
	
    Player.CharacterAdded:Connect(function()
    local Character = Player.Character
	local Races = {
		Human = 60, 
		Demon = 5,
		Elf = 20,
		Giant = 40;
	}

	local sum = 0
	for _,Values in pairs(Races) do
		sum = sum + Values
	end

	local function getRandomItem()
		local RaceValue = Random.new():NextNumber(0, sum)
		for item, value in pairs(Races) do
			RaceValue = RaceValue - value
			if RaceValue < 0 then
				return item
			end
		end
	end
			-- the function below changes the size of the players body (but in r15)

	local function ChangeBody()
		local RaceNames = Player.DataFolder.Race.Value

		local Parts = Character:GetChildren()
		
		
		local BodySizeParts = {
			Humanoid.HeadScale,
			Humanoid.BodyDepthScale,
			Humanoid.BodyWidthScale,
			Humanoid.BodyHeightScale,
		}
		

		if RaceNames == "Giant" then
			for _,v in pairs(BodySizeParts) do
				v.Value = 2    	 -- scale
			end
		elseif RaceNames == "Elf" then
			for _,v in pairs(BodySizeParts) do
				v.Value = 0.6
			end
		elseif RaceNames == "Demon" then
			for _,v in ipairs(Parts) do
				if v:IsA("BasePart") then
					v.BrickColor = BrickColor.new("Really black")
				end
			end
		end

	end

	local DataFolder = Instance.new('Folder',Player)
	DataFolder.Name = "DataFolder"

	local Race = Instance.new("StringValue", DataFolder)
	Race.Name = "Race"

	local function Change() 		--changes text in StarterGui > RaceDisplay > TextButton > TextButton (i think, not quite sure, line 75)
		PlayerGui.RaceDisplay.TextButton.TextButton.Text = "Race: "..Race.Value
	end

	local Success, Value = pcall(function()
		return DataSaving:GetAsync(Player.UserId)
	end)

	if Success then
		if Value then
			Race.Value = Value[1]
		else
			Race.Value = tostring(getRandomItem())
		end
	else
		Race.Value = tostring(getRandomItem())
	end
	
	ChangeBody() 	 -- activates functions
	Change()
		

	end)
end)

I made it so it fires everytime the player respawns, so it shouldn’ matter if they reset

1 Like

Thanks, but the script didn’t work, (it was missing some variables that I put back, but it still didn’t do anything). Thanks anyway! I also tried combining this with the first script just to be sure.

What variables did you put back? Some of them HAD to be in the bit I added

i put back a Character Variable, and thats all

Put it back inside the area I added. It has to be inside

I tried it, but it came with an error on the line 2

What is the error??? I can’t help you debug if you don’t provide the error

ā€˜ServerScriptService.Race1 (server script)’, Line 2 - Studio - Race1 (server script):2
23:30:55.902 Stack End - Studio

this is what came out

I don’t think that’s the entire error. Can you please screenshot the output

23:34:15.508 ServerScriptService.Race1 (server script):2: attempt to index nil with ā€˜WaitForChild’ - Server - Race1 (server script):2
23:34:15.508 Stack Begin - Studio
23:34:15.508 Script ā€˜ServerScriptService.Race1 (server script)’, Line 2 - Studio - Race1 (server script):2
23:34:15.508 Stack End - Studio

Also there is in the script analysis that says unknown global character

Can you please move local Humanoid = Character:WaitForChild("Humanoid") after the local character = bit within the bit I added

Ok, i put it where you said i need to put it,

game.Players.PlayerAdded:Connect(function(Player) 
	local Character = Player.Character
	local Humanoid = Character:WaitForChild("Humanoid") 
	local PlayerGui = Player:WaitForChild("PlayerGui") 

but there still an error, the same error

23:39:02.713 ServerScriptService.Race1 (server script):5: attempt to index nil with ā€˜WaitForChild’ - Server - Race1 (server script):5
23:39:02.713 Stack Begin - Studio
23:39:02.713 Script ā€˜ServerScriptService.Race1 (server script)’, Line 5 - Studio - Race1 (server script):5
23:39:02.713 Stack End - Studio

You moved them all out of the bit I added again…

local DataSaving = game:GetService("DataStoreService"):GetDataStore("Saving001")

game.Players.PlayerAdded:Connect(function(Player) 
Player.CharacterAdded:Connect(function()
	local Character = Player.Character
	local Humanoid = Character:WaitForChild("Humanoid") 
	local PlayerGui = Player:WaitForChild("PlayerGui") 
	
	
	local Races = {
		Human = 60, 
		Demon = 5,
		Elf = 20,
		Giant = 40;
	}

	local sum = 0
	for _,Values in pairs(Races) do
		sum = sum + Values
	end

	local function getRandomItem()
		local RaceValue = Random.new():NextNumber(0, sum)
		for item, value in pairs(Races) do
			RaceValue = RaceValue - value
			if RaceValue < 0 then
				return item
			end
		end
	end
			-- the function below changes the size of the players body (but in r15)

	local function ChangeBody()
		local RaceNames = Player.DataFolder.Race.Value

		local Parts = Character:GetChildren()
		
		
		local BodySizeParts = {
			Humanoid.HeadScale,
			Humanoid.BodyDepthScale,
			Humanoid.BodyWidthScale,
			Humanoid.BodyHeightScale,
		}
		

		if RaceNames == "Giant" then
			for _,v in pairs(BodySizeParts) do
				v.Value = 2    	 -- scale
			end
		elseif RaceNames == "Elf" then
			for _,v in pairs(BodySizeParts) do
				v.Value = 0.6
			end
		elseif RaceNames == "Demon" then
			for _,v in ipairs(Parts) do
				if v:IsA("BasePart") then
					v.BrickColor = BrickColor.new("Really black")
				end
			end
		end

	end

	local DataFolder = Instance.new('Folder',Player)
	DataFolder.Name = "DataFolder"

	local Race = Instance.new("StringValue", DataFolder)
	Race.Name = "Race"

	local function Change() 		--changes text in StarterGui > RaceDisplay > TextButton > TextButton (i think, not quite sure, line 75)
		PlayerGui.RaceDisplay.TextButton.TextButton.Text = "Race: "..Race.Value
	end

	local Success, Value = pcall(function()
		return DataSaving:GetAsync(Player.UserId)
	end)

	if Success then
		if Value then
			Race.Value = Value[1]
		else
			Race.Value = tostring(getRandomItem())
		end
	else
		Race.Value = tostring(getRandomItem())
	end
	
	ChangeBody() 	 -- activates functions
	Change()
		

	end)
end)


game.Players.PlayerRemoving:Connect(function(Player) 
	local Success, Err = pcall(function()
		DataSaving:SetAsync(Player.UserId, {Player.DataFolder:WaitForChild('Race').Value})
	end)
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		local Success, Err = pcall(function()
			DataSaving:SetAsync(player.UserId, {player.DataFolder:WaitForChild('Race').Value})
		end)
	end
end)
1 Like

Ohhh, i needed to put them in this… well i just tested it
it kinda works. (i am starting to feel bad for wasting your time) so after player respawns they were given a diffrent ā€œRaceā€, but i think i know how to fix it, ty!

If the answer is satisfactory, please ensure to mark it as the solution

1 Like