I tried to make a Value for telling me the Direction of what the Player Pushed but it always says…
Origin of the Value:
Error of the value:
part.Touched:Connect(function(hit)
if touching == false then
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
local Direct = player:FindFirstChild("Direction")
local InputDir = player:FindFirstChild("InputDir")
part.Touched:Connect(function(hit)
if touching == false then
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
local Direct = player:FindFirstChild("Direction")
local InputDir = player:FindFirstChild("InputDir")
part.Touched:Connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
if touching == false then
local Direct = player:FindFirstChild("Direction")
local InputDir = player:FindFirstChild("InputDir")
end
end
end)
part.Touched:Connect(function(hit)
if touching == false then
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
local Direct = player:FindFirstChild("Direction")
local InputDir = player:FindFirstChild("InputDir")
if player then
if Direct.Value == "Neutral" then
touching = true
game.Workspace.Gravity = 196.2 --Changes to normal Gravity while Flying Up
local bv = Instance.new("BodyVelocity")
bv.Name = "bounce"
bv.Velocity = Vector3.new(0,40,0) --change the y value
bv.MaxForce = bv.MaxForce * 50 --change this
bv.Parent = player.Character.HumanoidRootPart
task.wait(0.1)
bv:Destroy()
touching = false
Direct.Value = "Neutral"
game.Workspace.Gravity = 50 --Chnages Back to low gravity since Falling
end
elseif Direct.Value == "Left" then
if InputDir == "Left" then
touching = true
game.Workspace.Gravity = 196.2 --Changes to normal Gravity while Flying Up
local bv = Instance.new("BodyVelocity")
bv.Name = "bounce"
bv.Velocity = Vector3.new(-40,40,0) --change the y value + Left
bv.MaxForce = bv.MaxForce * 50 --change this
bv.Parent = player.Character.HumanoidRootPart
Direct.Value = "Neutral"
task.wait(0.1)
bv:Destroy()
touching = false
game.Workspace.Gravity = 50 --Chnages Back to low gravity since Falling
end
elseif Direct.Value == "Right"then
if InputDir == "Right" then
touching = true
game.Workspace.Gravity = 196.2 --Changes to normal Gravity while Flying Up
local bv = Instance.new("BodyVelocity")
bv.Name = "bounce"
bv.Velocity = Vector3.new(40,40,0) --change the y value + Right
bv.MaxForce = bv.MaxForce * 50 --change this
bv.Parent = player.Character.HumanoidRootPart
Direct.Value = "Neutral"
task.wait(0.1)
bv:Destroy()
touching = false
game.Workspace.Gravity = 50 --Chnages Back to low gravity since Falling
end
elseif InputDir.Value == "None" then
touching = true
game.Workspace.Gravity = 196.2 --Changes to normal Gravity while Flying Up
local bv = Instance.new("BodyVelocity")
bv.Name = "bounce"
bv.Velocity = Vector3.new(0,40,0) --change the y value
bv.MaxForce = bv.MaxForce * 50 --change this
bv.Parent = player.Character.HumanoidRootPart
task.wait(0.1)
bv:Destroy()
touching = false
Direct.Value = "Neutral"
game.Workspace.Gravity = 50 --Chnages Back to low gravity since Falling
end
else
end
end)
Ok, so GetPlayerFromCharacter(Model) will return a Player object (what you want) OR nil (what you don’t want)
While you can perform FindFirstChild on a Player object just fine, you cannot perform it on nil at all- in fact it will produce an error! That’s what’s happening here.
Before you try to do player:FindFirstChild, you must check if the player variable you stored, is NOT nil. There’s a lot of statements you could perform to do this, but I recommend if player then, which simply checks if the player variable is not nil and not false- we don’t really care if it checks for false because we know it can’t be, but it does check for nil! Yipee!
It’s important to check if it’s valid BEFORE you do ANYTHING to the player variable, because that’s why it’s erroring.
There’s one more minor mistake in your code, you’re using hit.Parent to look for the player- that’s totally normal and totally fine, but if a hat or whatever touches it that’s inside a character, we should probably check for that.
This is a tiny bit of code that pretty much performs what you wanna do.
part.Touched:Connect(function(hit)
if not touching then
-- are you doing something else?
return --// end code here
end
local character = hit:FindFirstAncestorOfClass("Model") --// finds the first parent or parent of parent that is a Model
if not character then
--// no model found- this can't be a character.
return --// end code
end
local player = game:GetService("Players"):GetPlayerFromCharacter(character)
if not player then
--// handle something if we detect not a player?
return --// end code
end
--// we know it's a player and the variable is not false!
local Direct = player:FindFirstChild("Direction")
--// etc etc
end)
You forgot to check if GetPlayerFromCharacter of hit.Parent is really a player or not.
If the part touches something like Baseplate or anything else it’d throw out an error.
The fixed ones will look something like this
part.Touched:Connect(function(hit)
if touching == false then
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
local Direct = player:FindFirstChild("Direction")
local InputDir = player:FindFirstChild("InputDir")
part.Touched:Connect(function(hit)
if not touching then
-- are you doing something else?
return --// end code here
end
local character = hit:FindFirstAncestorOfClass("Model") --// finds the first parent or parent of parent that is a Model
if not character then
--// no model found- this can't be a character.
return --// end code
end
local player = game:GetService("Players"):GetPlayerFromCharacter(character)
if not player then
--// handle something if we detect not a player?
return --// end code
end
--// we know it's a player and the variable is not false!
local Direct = player:FindFirstChild("Direction")
--// etc etc
if Direct.Value == "Neutral" then
touching = true
game.Workspace.Gravity = 196.2 --Changes to normal Gravity while Flying Up
local bv = Instance.new("BodyVelocity")
bv.Name = "bounce"
bv.Velocity = Vector3.new(0,40,0) --change the y value
bv.MaxForce = bv.MaxForce * 50 --change this
bv.Parent = player.Character.HumanoidRootPart
task.wait(0.1)
bv:Destroy()
touching = false
Direct.Value = "Neutral"
game.Workspace.Gravity = 50 --Chnages Back to low gravity since Falling
end
elseif Direct.Value == "Left" then
if InputDir == "Left" then
touching = true
game.Workspace.Gravity = 196.2 --Changes to normal Gravity while Flying Up
local bv = Instance.new("BodyVelocity")
bv.Name = "bounce"
bv.Velocity = Vector3.new(-40,40,0) --change the y value + Left
bv.MaxForce = bv.MaxForce * 50 --change this
bv.Parent = player.Character.HumanoidRootPart
Direct.Value = "Neutral"
task.wait(0.1)
bv:Destroy()
touching = false
game.Workspace.Gravity = 50 --Chnages Back to low gravity since Falling
end
elseif Direct.Value == "Right"then
if InputDir == "Right" then
touching = true
game.Workspace.Gravity = 196.2 --Changes to normal Gravity while Flying Up
local bv = Instance.new("BodyVelocity")
bv.Name = "bounce"
bv.Velocity = Vector3.new(40,40,0) --change the y value + Right
bv.MaxForce = bv.MaxForce * 50 --change this
bv.Parent = player.Character.HumanoidRootPart
Direct.Value = "Neutral"
task.wait(0.1)
bv:Destroy()
touching = false
game.Workspace.Gravity = 50 --Chnages Back to low gravity since Falling
end
elseif InputDir.Value == "None" then
touching = true
game.Workspace.Gravity = 196.2 --Changes to normal Gravity while Flying Up
local bv = Instance.new("BodyVelocity")
bv.Name = "bounce"
bv.Velocity = Vector3.new(0,40,0) --change the y value
bv.MaxForce = bv.MaxForce * 50 --change this
bv.Parent = player.Character.HumanoidRootPart
task.wait(0.1)
bv:Destroy()
touching = false
Direct.Value = "Neutral"
game.Workspace.Gravity = 50 --Chnages Back to low gravity since Falling
end
else
end
end)
end)
part.Touched:Connect(function(hit)
if touching == false then
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
Direct = player.Direction
InputDir = player.InputDir
if Direct.Value == "Neutral" then
touching = true
game.Workspace.Gravity = 196.2 --Changes to normal Gravity while Flying Up
local bv = Instance.new("BodyVelocity")
bv.Name = "bounce"
bv.Velocity = Vector3.new(0,40,0) --change the y value
bv.MaxForce = bv.MaxForce * 50 --change this
bv.Parent = player.Character.HumanoidRootPart
task.wait(0.1)
bv:Destroy()
touching = false
Direct.Value = "Neutral"
game.Workspace.Gravity = 50 --Chnages Back to low gravity since Falling
end
elseif Direct.Value == "Left" then
if InputDir == "Left" then
touching = true
game.Workspace.Gravity = 196.2 --Changes to normal Gravity while Flying Up
local bv = Instance.new("BodyVelocity")
bv.Name = "bounce"
bv.Velocity = Vector3.new(-40,40,0) --change the y value + Left
bv.MaxForce = bv.MaxForce * 50 --change this
bv.Parent = player.Character.HumanoidRootPart
Direct.Value = "Neutral"
task.wait(0.1)
bv:Destroy()
touching = false
game.Workspace.Gravity = 50 --Chnages Back to low gravity since Falling
end