Use a vector obj_val = Float64[] to store the values, and use push! to add elements to the vector:

using JuMP, Plots, Gurobi
function run_code()
model = Model(with_optimizer(Gurobi.Optimizer))
@variable(model, x >= 0)
@variable(model, y >= 0)
@constraint(model, 2x - 4 >= 0)
@constraint(model, y - 0.5*x >= 0)
k = 0
obj_val = Float64[]
while k <= 1
@objective(model, Max, 3x + k * y)
optimize!(model)
k = k + 0.2
push!(obj_val, objective_value(model))
end
return obj_val
end
run_code()