No idea how to explain, something about tables

Alright so basically, i got a script in which it has a table full of words, everytime a player joins it picks a certain word, and the word is a value of an ObjectValue, and the script checks if the value is (for example) “Fast” then the player’s speed is set to 20.

game.Players.PlayerAdded:Connect(function(player)
	local Events = {
		"Deadly Disease",
		"Giant",
		"Small",
		"Very Old",
		"Young",
		"Fast",
		"Slow",
		"High jump",
		"Small jump"
	}
	
	
	local EventsValue = game:GetService("ServerStorage").Events_value
	EventsValue.Value = Events[math.random(0, #Events)]
	print(EventsValue.Value)
	if EventsValue.Value == "Fast" then
		player.Character:FindFirstChild("Humanoid").WalkSpeed = 20
	elseif EventsValue.Value == "Very Old" then
		player.Character:FindFirstChild("Humanoid").WalkSpeed = 6
	
	
	end
end)

everything works but when the word 'Fast" is selected i get

ServerScriptService.Script:19: attempt to index nil with ‘FindFirstChild’

It’s because the humanoid hasn’t loaded yet, even though the player is registered in the game the humanoid might still be loading you can fix this with wait for child or adding a delay.

i get “attempt to index with waitforchild”

The error is indicating that you’re calling nil:FindFirstChild(), which means that the player’s Character doesn’t exist when the script is trying to reference it.

To resolve this, you can create a variable for the Character that references it if it exists, or waits for it to load:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    local Character = player.Character or player.CharacterAdded:Wait()
    -- Continue
end)

i tried that but now i get “Unable to assign property Value. string expected, got nil” whenever i join, the problem is this line:

EventsValue.Value = Events[math.random(0, #Events)]

PlayerAdded is fired when the player joins the game. they dont have a character at this point

run all this code with player.CharacterAdded

It worked, also thanks to the other people trying to help

The random number it chose might’ve been 0, which could have caused that error because the Events table wouldn’t have anything stored at that index (because indexes for arrays start at 1). Updating that to the following should resolve that error:

EventsValue.Value = Events[math.random(1, #Events)]
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.