Character error

in my scrippt, theres this error:

Workspace.Script:2: attempt to index nil with ‘Character’ - Server - Script:2

can someone help fix this please:

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local flying = false

humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
	if humanoid.FloorMaterial == Enum.Material.Air then
		flying = true
		print(player.Name .. " is now flying!")
	else
		flying = false
		print(player.Name .. " is no longer flying.")
	end
end)

Did you forget to add:

local player = game.Players.LocalPlayer

Are you using the Correct Script to do so??

sorry i forgot to also put that line, it was already there, but thats not why its not working.
What do you mean?

Try adding this to the top of your code:

repeat wait() until game:IsLoaded()
1 Like

Using game.Players.LocalPlayer would require the use of a LocalScript. A player’s character would have to be found different using a ServerScript. I believe this is what he means.

1 Like

Since you are using a regular script, you would need to use:

local Player = game.Players:GetPlayerFromCharacter()

to get the player’s character.

would this be good ?

local player = game.Players.LocalPlayer
local character = game.Players:GetPlayerFromCharacter()
local humanoid = character:WaitForChild("Humanoid")
local flying = false

humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
	if humanoid.FloorMaterial == Enum.Material.Air then
		flying = true
		print(player.Name .. " is now flying!")
	else
		flying = false
		print(player.Name .. " is no longer flying.")
	end
end)
1 Like

I haven’t tested, but give it a try and let me know what happens next.

nothing happens, is there a way to maybe make that the script check the player’s humanoid to see if he’s flying?

You cannot actually get the local player within a server-script you can only do that in a LocalScript hence why its not working when you reference their character.

This thread may help:

1 Like

game.Players.LocalPlayer doesn’t work in a server script, so you should use other means to get the Player. For example:
local player = game.Players[player's name]
or
local player = game.Players:GetPlayerFromCharacter(player's character)

You are trying to get a local player from a server-script which is impossible and can only be done on a local-script.

What you should be doing is checking the character of a player that joins or gets added to the server.

local Players = game:GetService("Players")
local flying = false

Players.PlayerAdded:Connect(function(Player)
	local character = Player.Character or Player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	
	humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
		if humanoid.FloorMaterial == Enum.Material.Air then
			flying = true
			print(Player.Name .. " is now flying!")
		else
			flying = false
			print(Player.Name .. " is no longer flying.")
		end
	end)
end)

or

local Players = game:GetService("Players")
local flying = false

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local humanoid = Character:WaitForChild("Humanoid")

		humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
			if humanoid.FloorMaterial == Enum.Material.Air then
				flying = true
				print(Player.Name .. " is now flying!")
			else
				flying = false
				print(Player.Name .. " is no longer flying.")
			end
		end)
	end)
end)

I made a new script for this and i fixed the problems, i also added a webhook system that send a message when player is caught speed hacking

im getting this issue:

here is the script:

local HttpService = game:GetService("HttpService")
local playerCaught = {}

-- Replace the webhook URL below with your own Discord webhook URL
local webhookUrl = "https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

while true do
	local player = game.Players.LocalPlayer

	wait(0.5) -- dont delete this or script can crash
	if player.Character.Humanoid.WalkSpeed > 16 then -- if you have any shift to sprint scripts change 16 to value of running speed
		if player.Name == "Insert admin name here" or playerCaught[player.Name] then -- this protects owner and admins from getting kicked (to add more just add 'or player.Name == "Name here"' before then)
			-- do nothing
		else
			playerCaught[player.Name] = true -- set the player as caught

			-- send message to Discord webhook
			local message = {
				["content"] = player.Name .. " is speed hacking!"
			}
			local encodedMessage = HttpService:JSONEncode(message)
			HttpService:PostAsync(webhookUrl, encodedMessage)

			print(player.Name.." is speed hacking") --this kicks player. you can change the message
		end
	end
end

You can’t use httpservice on local scripts and also Discord blocked Roblox servers. This means Roblox servers can’t access discord webhooks anymore. I would also not recommend creating an anticheat on a local script as exploiters can easily access that and just delete the script.

Why cant they access it anymore?

Discord blocked them because of webhook spammers.

isnt there any roblox games that use it?

i don’t know but all i know is they blocked them.

You can only use HTTP Service on the server, not the client.

well now im getting this error since its in server script, cant find character