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)
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.
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)
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.
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)
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.