So i am new to OOP, especially in lua and im just generally trying to use it wherever i can. The problem is that right now, it seems that i cant change a value of a variable i stored in an object. Also, slightly unrelated but how do you add variables to an object whilst not putting them in the curly brackets?
Creaturedata = {preference = 0,currentpos = nil,nextpos = nil,speedval = 20}
-- The creation of the object
Box.Touched:Connect(function(hit,Creaturedata)
wait(0.2)
if hit.Parent == character then
while task.wait(0.5) do
local raystart = chosencreature.CFrame.Position
local rayend = workspace.Camera.CFrame.Position
local raydirect = rayend - raystart
local raycast = workspace:Raycast(raystart,raydirect)
print("Distance:", raycast.Distance)
Creaturedata.speedval = raycast.Distance/2 -- This is the part that errors, and says that speedval doesn't exist
end
end
end)
As they said before, Touched only returns one value, if you try to enter another value it will be nil, hence the error.
attempt to index nil with speedval
Also raycast can give nil sometimes, so it is better to check it:
Creaturedata = {preference = 0,currentpos = nil,nextpos = nil,speedval = 20}
-- The creation of the object
Box.Touched:Connect(function(hit)
task.wait(0.2)
if hit.Parent == character then
while task.wait(0.5) do
local raystart = chosencreature.CFrame.Position
local rayend = workspace.Camera.CFrame.Position
local raydirect = rayend - raystart
local raycast = workspace:Raycast(raystart,raydirect)
if raycast then
print("Distance:", raycast.Distance)
Creaturedata.speedval = raycast.Distance/2
else
warn("hey, no raycast")
end
end
end
end)