Need help with a function that returns on using water

Bassicly i need help on how to check if the player is pressing e while looking at water
this is what ive got so far

if Player:KeyDown(IN_USE)
then
if local trace = {}
trace.start = groundtrace.HitPos
trace.endpos = trace.start + Vector( 0, 0, 1 )
trace.mask = MASK_WATER Player:KeyDown(IN_USE)
then ply:thingymajig
end
end
Its sadly giving lua errors and i cant check them right now so i will post them when iam able to because my console is all messed up


if local trace = {}

that is not how lua works

Use the GetEyeTrace function in the ENTITY:Use method.

download gmod stranded and see how they did it

Wow… did you even look at the wiki?

Like this?

hook.Add( “KeyPress”, “IN_USE”, function( ply, key )
if ( key != IN_USE ) then return end
if ( ply:KeyDown( 1 ) ) then return end
local trace = {}
trace.start = ply:GetShootPos()
trace.endpos = trace.start + ( ply:GetAimVector() * 150 )
trace.mask = bit.bor( MASK_WATER, MASK_SOLID )
trace.filter = ply

local tr2 = util.TraceLine( trace )
if ( ( tr2.Hit && tr2.MatType == MAT_SLOSH && ply:WaterLevel() > 0 ) or ply:WaterLevel() == 3 ) then
ply:test
end
end

I’m 60% sure he think it’s working like in C, where you have to check if the malloc worked.

But not sure why he tried to make it local at the same time.

Maybe because C++ allows this bullshit:



if(void* shit = malloc(123)) { ... }