I’m trying to make a script that pushes players away from me but there’s an error that appears that says attempt to index nil with ‘Character’ and I’m so confused because everything looks like it should work in the script!
I have tried some solutions which are removing large parts of the script but not the parts of the script that are highly necessary but it still doesn’t work and I haven’t looked for solutions on Devforum because I have absolutely no idea what to search for
Here’s the script
local ShockwaveEvent = game.ReplicatedStorage.AdminGuiEvents:WaitForChild("ShockwaveEvent")
ShockwaveEvent.OnServerEvent:Connect(function()
wait(0.1)
for _,player in ipairs(game.Players:GetPlayers()) do
local character = player.Character
local killersuperlegend = player:FindFirstChild("killersuperlegend")
local lookpart = killersuperlegend.Character.HumanoidRootPart
character.HumanoidRootPart.CFrame = CFrame.new(character.HumanoidRootPart.CFrame.Position, Vector3.new(lookpart.Position.X,character.HumanoidRootPart.Position.Y,lookpart.Position.Z))
for i = 1, 25 do
task.wait()
character.HumanoidRootPart.CFrame = character.HumanoidRootPart.CFrame - (character.HumanoidRootPart.CFrame.LookVector)
end
end
end)
I tried switching lookpart to a part in the workspace and everything worked completely fine! So I have no idea why that error is occurring!
How do I fix this?
You’re calling for killersuperlegend inside the player…? EX: game.Players.stwqbrri3s.killersuperlegend which is nil because its not a thing.
Make sure you’re always trying to get another player from game.Players
Here’s a fixed version.
local ShockwaveEvent = game.ReplicatedStorage.AdminGuiEvents:WaitForChild("ShockwaveEvent")
ShockwaveEvent.OnServerEvent:Connect(function()
wait(0.1)
for _,player in ipairs(game.Players:GetPlayers()) do
local killersuperlegend = game.Players:FindFirstChild("killersuperlegend")
local character = player.Character
local lookpart = killersuperlegend.Character.HumanoidRootPart
character.HumanoidRootPart.CFrame = CFrame.new(character.HumanoidRootPart.CFrame.Position, Vector3.new(lookpart.Position.X,character.HumanoidRootPart.Position.Y,lookpart.Position.Z))
for i = 1, 25 do
task.wait()
character.HumanoidRootPart.CFrame = character.HumanoidRootPart.CFrame - (character.HumanoidRootPart.CFrame.LookVector)
end
end
end)
local ShockwaveEvent = game.ReplicatedStorage.AdminGuiEvents:WaitForChild("ShockwaveEvent")
ShockwaveEvent.OnServerEvent:Connect(function()
for _,player in ipairs(game.Players:GetPlayers()) do
repeat wait(.1)
until player.Character
local character = player.Character
local killersuperlegend = player:FindFirstChild("killersuperlegend")
local lookpart = killersuperlegend.Character.HumanoidRootPart
character.HumanoidRootPart.CFrame = CFrame.new(character.HumanoidRootPart.CFrame.Position, Vector3.new(lookpart.Position.X,character.HumanoidRootPart.Position.Y,lookpart.Position.Z))
for i = 1, 25 do
task.wait()
character.HumanoidRootPart.CFrame = character.HumanoidRootPart.CFrame - (character.HumanoidRootPart.CFrame.LookVector)
end
end
end)
That code sadly didn’t work either, but your code made me come up with a possible solution, and it was instead looking for my character in Players, I made the code look for my character inside the workspace, and it worked!
local ShockwaveEvent = game.ReplicatedStorage.AdminGuiEvents:WaitForChild("ShockwaveEvent")
ShockwaveEvent.OnServerEvent:Connect(function()
for _,player in ipairs(game.Players:GetPlayers()) do
local character = player.Character or player:CharacterAdded:Wait
local killersuperlegend = player:FindFirstChild("killersuperlegend")
local lookpart = killersuperlegend.Character.HumanoidRootPart
character.HumanoidRootPart.CFrame = CFrame.new(character.HumanoidRootPart.CFrame.Position, Vector3.new(lookpart.Position.X,character.HumanoidRootPart.Position.Y,lookpart.Position.Z))
for i = 1, 25 do
task.wait()
character.HumanoidRootPart.CFrame = character.HumanoidRootPart.CFrame - (character.HumanoidRootPart.CFrame.LookVector)
end
end
end)
local ShockwaveEvent = game.ReplicatedStorage.AdminGuiEvents:WaitForChild("ShockwaveEvent")
ShockwaveEvent.OnServerEvent:Connect(function(player)
wait(0.1)
for _,player in ipairs(game.Players:GetPlayers()) do
local character = game.Workspace:FindFirstChild(player.Name)
local killersuperlegend = player:FindFirstChild("killersuperlegend")
local lookpart = game.Workspace:FindFirstChild("killersuperlegend").HumanoidRootPart
character.HumanoidRootPart.CFrame = CFrame.new(character.HumanoidRootPart.CFrame.Position, Vector3.new(lookpart.Position.X,character.HumanoidRootPart.Position.Y,lookpart.Position.Z))
for i = 1, 25 do
task.wait()
character.HumanoidRootPart.CFrame = character.HumanoidRootPart.CFrame - (character.HumanoidRootPart.CFrame.LookVector)
end
end
end)
I would have to see the full script to understand your problem. But, I think the main reason is that it is not waiting for the player to join the server, and therefore it cannot find the Character property.